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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
# File 'lib/has_eav.rb', line 99

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)


175
176
177
178
179
180
181
# File 'lib/has_eav.rb', line 175

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



85
86
87
# File 'lib/has_eav.rb', line 85

def class_eav_attributes
  self.class.class_eav_attributes
end

#eav_attributes_listObject

get a complete list of eav_attributes (class + instance)



184
185
186
187
188
# File 'lib/has_eav.rb', line 184

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



80
81
82
# File 'lib/has_eav.rb', line 80

def eav_class
  self.class.eav_class
end

#instance_eav_attributesObject

Override this to get some usable attributes

Cowardly refusing to adhere to all



92
93
94
# File 'lib/has_eav.rb', line 92

def instance_eav_attributes
  []
end

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

override respond_to?

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
159
# File 'lib/has_eav.rb', line 150

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



202
203
204
205
206
207
208
209
# File 'lib/has_eav.rb', line 202

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

  hash
end