Module: Cardboard::Seed
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/cardboard/helpers/seed.rb
Class Method Summary collapse
- .populate_fields(fields, object) ⇒ Object
- .populate_pages(pages) ⇒ Object
- .populate_parts(page_parts, db_page) ⇒ Object
- .populate_settings(settings) ⇒ Object
- .populate_template(id, template) ⇒ Object
- .populate_templates(templates) ⇒ Object
Class Method Details
.populate_fields(fields, object) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cardboard/helpers/seed.rb', line 55 def self.populate_fields(fields, object) fields ||= {} fields.each do |id, field| db_field = Field.where(identifier: id.to_s, object_with_field: object).first_or_initialize db_field.type = "Cardboard::Field::#{(field[:type] || "string").camelize}" db_field.seeding = true begin db_field.update_attributes!(field.select{|k,v| ["default", "value"].include?(k)}) rescue Exception => e # Output validation errors puts "-- ERROR --" puts e puts "#{db_field.identifier}: #{db_field.value_uid || field[:value] || field[:default] || "nil"}" puts "-----------" end end #remove fields no longer in the seed file # for remove_field in object.fields.map(&:identifier) - fields.map{|k,v|k.to_s} # object.fields.where(identifier: remove_field).first.destroy # end end |
.populate_pages(pages) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cardboard/helpers/seed.rb', line 24 def self.populate_pages(pages) pages ||= {} # add the page pages.each do |id, page| # create the template db_template = populate_template(id, page.merge(is_page: true)) db_page = Cardboard::Page.where(identifier: id.to_s).first_or_initialize db_page.position_position = page[:position] || :last db_page.template = db_template db_page.update_attributes!(page.slice(:title, :parent_id)) self.populate_parts(page[:parts], db_page) end end |
.populate_parts(page_parts, db_page) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cardboard/helpers/seed.rb', line 41 def self.populate_parts(page_parts, db_page) # called from the page controller page_parts ||= {} page_parts.each do |id, part| db_part = db_page.parts.where(identifier: id.to_s).first_or_create self.populate_fields(part[:fields], db_part) end # for remove_part in db_page.parts.map(&:identifier) - page_parts.map{|k,v|k.to_s} # db_page.parts.where(identifier: remove_part).first.destroy # end end |
.populate_settings(settings) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/cardboard/helpers/seed.rb', line 78 def self.populate_settings(settings) settings ||= {} settings["company_name"] ||= {type: "string", default: Cardboard.application.site_title} db_settings = Cardboard::Setting.first_or_create db_settings.update_attributes!(template: settings) self.populate_fields(settings, db_settings) end |
.populate_template(id, template) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/cardboard/helpers/seed.rb', line 8 def self.populate_template(id, template) db_template = Cardboard::Template.where(identifier: id.to_s).first_or_initialize db_template.update_attributes!(name: template[:title] || template[:name] || id.to_s, fields: template[:parts], controller_action: template[:controller_action], is_page: template[:is_page]) db_template end |
.populate_templates(templates) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/cardboard/helpers/seed.rb', line 17 def self.populate_templates(templates) templates ||= {} templates.each do |id, template| db_template = populate_template(id, template.merge(is_page: false)) end end |