Module: Cms::DataLoader

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


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cms/data_loader.rb', line 6

def method_missing(method_name, *args)
  if md = method_name.to_s.match(/^create_(.+)$/)
    # We search the CMS namespace first.
    # for things like DynamicPortlets "Cms::DynamicPortlet".constantize returns "DynamicPortlet"
    model_name = "Cms/#{md[1]}".classify.constantize.name        
    begin
      #Make sure this is an active record class
      super unless model_name.classify.constantize.ancestors.include?(ActiveRecord::Base)
    rescue NameError => e
      super
    end
    self.create(model_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


25
26
27
28
29
30
31
32
33
# File 'lib/cms/data_loader.rb', line 25

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