Module: ActiveRecord::ActsAs::HasEav::InstanceMethods

Defined in:
lib/has_eav.rb

Overview

/ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args, &block) ⇒ Object

override method missing, but only kick in when super fails with a NoMethodError



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/has_eav.rb', line 114

def method_missing method_symbol, *args, &block
  super
rescue NoMethodError => e
  method_name    = method_symbol.to_s
  attribute_name = method_name.gsub(/[\?=]$/, '')

  raise e unless eav_attributes_list.include? attribute_name

  attribute = self.eav_attributes.select { |a|
    a.name == attribute_name
  }.first

  if method_name =~ /\=$/
    value = args[0]

    if attribute
      if !value.nil?
        return attribute.send(:write_attribute, "value", value)

      else
        self.eav_attributes -= [ attribute ]
        return attribute.destroy

      end

    elsif !value.nil?
      self.eav_attributes << eav_class.new(
        :name  => attribute_name,
        :value => "#{value}"
      )

      return cast_eav_value(value, attribute_name)

    else
      return nil

    end
  elsif method_name =~ /\?$/
    return ( attribute and attribute.value == true ) ? true : false

  else
    return nil if attribute and attribute.destroyed?
    return attribute ?
      cast_eav_value(attribute.value, attribute_name) :
      nil
  end

  raise e
end

Instance Method Details

#changed?Boolean

override changed - if any of the eav_attributes has changed, the object has changed.

Returns:

  • (Boolean)


190
191
192
193
194
195
196
# File 'lib/has_eav.rb', line 190

def changed?
  eav_attributes.each do |attribute|
    return true if ( attribute.changed? || attribute.new_record? )
  end

  super
end

#class_eav_attributesObject

get the class eav attributes



100
101
102
# File 'lib/has_eav.rb', line 100

def class_eav_attributes
  self.class.class_eav_attributes
end

#eav_attributes_listObject

get a complete list of eav_attributes (class + instance)



199
200
201
202
203
# File 'lib/has_eav.rb', line 199

def eav_attributes_list # :nodoc:
  (
    self.instance_eav_attributes + self.class_eav_attributes.keys
  ).collect { |attribute| attribute.to_s }.uniq
end

#eav_classObject

get to the eav class



95
96
97
# File 'lib/has_eav.rb', line 95

def eav_class
  self.class.eav_class
end

#instance_eav_attributesObject

Override this to get some usable attributes

Cowardly refusing to adhere to all



107
108
109
# File 'lib/has_eav.rb', line 107

def instance_eav_attributes
  []
end

#respond_to?(method_symbol, is_private = false) ⇒ Boolean

override respond_to?

Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
173
174
# File 'lib/has_eav.rb', line 165

def respond_to? method_symbol, is_private=false
  if super == false
    method_name = method_symbol.to_s.gsub(/[\?=]$/, '')
    return true if eav_attributes_list.include? method_name

    false
  else
    true
  end
end

#serializable_hash(options = nil) ⇒ Object

make sure EAV is included in as_json, to_json and to_xml



217
218
219
220
221
222
223
224
# File 'lib/has_eav.rb', line 217

def serializable_hash options=nil
  hash = super
  eav_attributes_list.each do |attribute|
    hash[attribute] = self.send(attribute)
  end

  hash
end