Method: Netzke::DataMapper::Attributes#value_for_attribute
- Defined in:
- lib/netzke/data_mapper/attributes.rb
#value_for_attribute(a, through_association = false) ⇒ Object
Fetches the value specified by an (association) attribute If through_association is true, get the value of the association by provided method, not the associated record’s id E.g., author__name with through_association set to true may return “Vladimir Nabokov”, while with through_association set to false, it’ll return author_id for the current record
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/netzke/data_mapper/attributes.rb', line 192 def value_for_attribute(a, through_association = false) v = if a[:getter] a[:getter].call(self) elsif respond_to?("#{a[:name]}") send("#{a[:name]}") elsif is_association_attr?(a) split = a[:name].to_s.split(/\.|__/) assoc = self.class.relationships[split.first.to_sym] if through_association split.inject(self) do |r,m| # TODO: do we really need to descend deeper than 1 level? if r.respond_to?(m) r.send(m) else ::Rails.logger.debug "Netzke::Basepack: Wrong attribute name: #{a[:name]}" unless r.nil? nil end end else self.send assoc.child_key.first.name end end # a work-around for to_json not taking the current timezone into account when serializing ActiveSupport::TimeWithZone v = v.to_datetime.to_s(:db) if [ActiveSupport::TimeWithZone].include?(v.class) v = v.to_s(:db) if [DateTime, Date].include?(v.class) v end |