Module: TwoFaced::ModelExtensions

Defined in:
lib/two_faced/model_extensions.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/two_faced/model_extensions.rb', line 58

def method_missing(method_name, *args)
  prefix = "#{self.class.overwrite_attribute_prefix}_"
  if method_name.to_s[prefix] != nil
    original_method = method_name.to_s.sub(prefix, "")
    send original_method
  else
    super
  end

end

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/two_faced/model_extensions.rb', line 5

def self.included(base)
  base.attr_accessible :overrides_attributes
  base.has_many :overrides, :as => :overrideable, :dependent => :destroy
  base.accepts_nested_attributes_for :overrides
  base.after_find :get_overrides_for_context
  base.extend(ClassMethods)
end

Instance Method Details

#get_overrides_for_contextObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/two_faced/model_extensions.rb', line 45

def get_overrides_for_context
  klass = self.class
  self.overrides.where("context_name" => klass.context).each do |override|
    singleton_class.class_eval { attr_accessor "#{klass.overwrite_attribute_prefix}_#{override.field_name}" }
    send "#{klass.overwrite_attribute_prefix}_#{override.field_name}=", override.field_value
    if self.class.overwrite_attributes?
      send "#{override.field_name}=", override.field_value
      send "readonly!"
    end
  end
  self
end