Module: Cms::DataLoader
- Defined in:
- lib/cms/data_loader.rb
Overview
A DSL for creating CMS content in seed data. Creating content in this manner will log the creation of the record and store any created records in a hash. To use, add the following to your seeds.rb. require “cms/data_loader”
Instance Method Summary collapse
- #create(model_name, record_name, data = {}) ⇒ Object
- #method_missing(method_name, *args) ⇒ Object
-
#model_class(model_name) ⇒ Object
We search the CMS namespace first.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cms/data_loader.rb', line 16 def method_missing(method_name, *args) if md = method_name.to_s.match(/^create_(.+)$/) klass = model_class(md[1]) self.create(klass.name, args[0], args[1] || {}) elsif @data && @data.has_key?(method_name) record = @data[method_name][args.first] record ? record.class.find(record.id) : nil else super end end |
Instance Method Details
#create(model_name, record_name, data = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/cms/data_loader.rb', line 43 def create(model_name, record_name, data={}) puts "-- create_#{model_name}(:#{record_name})" unless Cms::DataLoader.silent_mode @data ||= {} model_storage_name = model_name.demodulize.underscore.pluralize.to_sym @data[model_storage_name] ||= {} model = model_name.classify.constantize.new(data) model.save! @data[model_storage_name][record_name] = model end |
#model_class(model_name) ⇒ Object
We search the CMS namespace first. for things like DynamicPortlets “Cms::DynamicPortlet”.constantize returns “DynamicPortlet”
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cms/data_loader.rb', line 30 def model_class(model_name) klass = begin "Cms/#{model_name}".classify.constantize rescue NameError => e model_name.classify.constantize end unless klass.method_defined?(:save!) raise "Can't create an instance of #{klass} because its not an ActiveRecord instance." end klass end |