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



95
96
97
98
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
# File 'lib/has_eav.rb', line 95

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
        return attribute.send(:write_attribute, "value", value)

      elsif value.nil?
        return attribute.destroy

      end

    else
      @eav_attributes << eav_class.new(
        :name  => attribute_name,
        :value => "#{value}"
      )

      return cast_eav_value(value, attribute_name)

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

  else
    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)


178
179
180
181
182
183
184
# File 'lib/has_eav.rb', line 178

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



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

def class_eav_attributes
  self.class.class_eav_attributes
end

#eav_attributesObject

get all the eav attribute instances available for this model instance

eg: if you model says ‘has_eav :through => :post_attribute’ these are all PostAttribute’s



158
159
160
161
162
# File 'lib/has_eav.rb', line 158

def eav_attributes
  @eav_attributes ||= eav_class.all(
    :conditions => { self_key => self.id }
  )
end

#eav_attributes_listObject

get a complete list of eav_attributes (class + instance)



187
188
189
190
191
# File 'lib/has_eav.rb', line 187

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



76
77
78
# File 'lib/has_eav.rb', line 76

def eav_class
  self.class.eav_class
end

#instance_eav_attributesObject

Override this to get some usable attributes

Cowardly refusing to adhere to all



88
89
90
# File 'lib/has_eav.rb', line 88

def instance_eav_attributes
  []
end

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

override respond_to?

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
# File 'lib/has_eav.rb', line 142

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