Module: HasMagicColumns::ActiveRecord::InstanceMethods

Defined in:
lib/has_magic_columns/active_record.rb

Overview

:nodoc:

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object (private)

Load (lazily) MagicAttributes or fall back



105
106
107
108
109
110
111
112
113
114
# File 'lib/has_magic_columns/active_record.rb', line 105

def method_missing(method_id, *args)
  super(method_id, *args)
rescue NoMethodError
  method_name = method_id.to_s
  attr_names = magic_columns.map(&:name)
  initialize_magic_columns and retry if attr_names.include?(method_name) or 
    (md = /[\?|\=]/.match(method_name) and 
    attr_names.include?(md.pre_match))
  super(method_id, *args)
end

Instance Method Details

#reload_with_magicObject

Reinitialize MagicColumns and MagicAttributes when Model is reloaded



63
64
65
66
# File 'lib/has_magic_columns/active_record.rb', line 63

def reload_with_magic
  initialize_magic_columns
  reload_without_magic
end

#update_attributes(new_attributes) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/has_magic_columns/active_record.rb', line 68

def update_attributes(new_attributes)
  attributes = new_attributes.stringify_keys
  magic_attrs = magic_columns.map(&:name)

  super(attributes.select{ |k, v| !magic_attrs.include?(k) })
  attributes.select{ |k, v| magic_attrs.include?(k) }.each do |k, v|
    col = find_magic_column_by_name(k)
    attr = find_magic_attribute_by_column(col).first
    attr.update_attributes(:value => v)
  end
end