Module: SeedsHelper
- Defined in:
- app/helpers/seeds_helper.rb
Class Method Summary collapse
-
.files(seed_name) ⇒ Array<String>
Gets the list of seed file paths.
-
.prioritize(types, files) ⇒ Hash<String, Class>
Prioritizes models such that dependent ones are created after the specified ones.
-
.types(files) ⇒ Array<Class>
Parses model classes from the list of file paths.
Class Method Details
.files(seed_name) ⇒ Array<String>
Gets the list of seed file paths
5 6 7 8 9 10 11 12 13 14 |
# File 'app/helpers/seeds_helper.rb', line 5 def self.files(seed_name) find_glob = if seed_name.present? "db/seeds/**/#{seed_name.underscore}.yml" else 'db/seeds/**/*.yml' end # Get all seed files and determine their model types Dir.glob(Rails.root.join(find_glob)) end |
.prioritize(types, files) ⇒ Hash<String, Class>
Prioritizes models such that dependent ones are created after the specified ones
30 31 32 33 34 35 36 |
# File 'app/helpers/seeds_helper.rb', line 30 def self.prioritize(types, files) priority = [PostType, CloseReason, License, TagSet, PostHistoryType, User, Ability, CommunityUser, Filter] files.zip(types).to_h.sort do |a, b| (priority.index(a.second) || 999) <=> (priority.index(b.second) || 999) end.to_h end |
.types(files) ⇒ Array<Class>
Parses model classes from the list of file paths
19 20 21 22 23 24 |
# File 'app/helpers/seeds_helper.rb', line 19 def self.types(files) files.map do |f| basename = Pathname.new(f).relative_path_from(Pathname.new(Rails.root.join('db/seeds'))).to_s basename.gsub('.yml', '').singularize.classify.constantize end end |