Module: Cms::DataLoader

Included in:
InitialData
Defined in:
lib/cms/data_loader.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cms/data_loader.rb', line 3

def method_missing(method_name, *args)
  if md = method_name.to_s.match(/^create_(.+)$/)
    begin
      #Make sure this is an active record class
      super unless md[1].classify.constantize.ancestors.include?(ActiveRecord::Base)
    rescue NameError => e
      super
    end
    self.create(md[1], 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



19
20
21
22
23
24
25
26
# File 'lib/cms/data_loader.rb', line 19

def create(model_name, record_name, data={})
  puts "-- create_#{model_name}(:#{record_name})"
  @data ||= {}
  @data[model_name.pluralize.to_sym] ||= {}
  model = model_name.classify.constantize.new(data)
  model.save!
  @data[model_name.pluralize.to_sym][record_name] = model
end