Module: PropertySets::ActiveRecordExtension::AssociationExtensions

Defined in:
lib/property_sets/active_record_extension.rb

Instance Method Summary collapse

Instance Method Details

#association_classObject



160
161
162
# File 'lib/property_sets/active_record_extension.rb', line 160

def association_class
  @association_class ||= proxy_association.klass
end

#build_default(arg) ⇒ Object



115
116
117
# File 'lib/property_sets/active_record_extension.rb', line 115

def build_default(arg)
  build(:name => arg.to_s, :value => association_class.raw_default(arg))
end

#disable(arg) ⇒ Object



111
112
113
# File 'lib/property_sets/active_record_extension.rb', line 111

def disable(arg)
  send("#{arg}=", "0")
end

#enable(arg) ⇒ Object



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

def enable(arg)
  send("#{arg}=", "1")
end

#get(keys = []) ⇒ Object

Accepts an array of names as strings or symbols and returns a hash.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/property_sets/active_record_extension.rb', line 69

def get(keys = [])
  property_keys = if keys.empty?
    association_class.keys
  else
    association_class.keys & keys.map(&:to_s)
  end

  property_pairs = property_keys.map do |name|
    value = lookup_value(association_class.type(name), name)
    [name, value]
  end.flatten(1)
  HashWithIndifferentAccess[*property_pairs]
end

#lookup(arg) ⇒ Object

The finder method which returns the property if present, otherwise a new instance with defaults



140
141
142
143
144
145
146
147
148
149
# File 'lib/property_sets/active_record_extension.rb', line 140

def lookup(arg)
  instance   = lookup_without_default(arg)
  instance ||= build_default(arg)
  instance.value_serialized = property_serialized?(arg)

  owner = proxy_association.owner

  instance.send("#{association_class.owner_class_sym}=", owner) if owner.new_record?
  instance
end

#lookup_or_default(arg) ⇒ Object

This finder method returns the property if present, otherwise a new instance with the default value. It does not have the side effect of adding a new setting object.



153
154
155
156
157
158
# File 'lib/property_sets/active_record_extension.rb', line 153

def lookup_or_default(arg)
  instance = lookup_without_default(arg)
  instance ||= association_class.new(:value => association_class.raw_default(arg))
  instance.value_serialized = property_serialized?(arg)
  instance
end

#lookup_value(type, key) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/property_sets/active_record_extension.rb', line 123

def lookup_value(type, key)
  serialized = property_serialized?(key)

  if instance = lookup_without_default(key)
    instance.value_serialized = serialized
    PropertySets::Casting.read(type, instance.value)
  else
    value = association_class.default(key)
    if serialized
      PropertySets::Casting.deserialize(value)
    else
      PropertySets::Casting.read(type, value)
    end
  end
end

#lookup_without_default(arg) ⇒ Object



119
120
121
# File 'lib/property_sets/active_record_extension.rb', line 119

def lookup_without_default(arg)
  detect { |property| property.name.to_sym == arg.to_sym }
end

#protected?(arg) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/property_sets/active_record_extension.rb', line 103

def protected?(arg)
  lookup(arg).protected?
end

#save(*args) ⇒ Object



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

def save(*args)
  each { |p| p.save(*args) }
end

#save!(*args) ⇒ Object



99
100
101
# File 'lib/property_sets/active_record_extension.rb', line 99

def save!(*args)
  each { |p| p.save!(*args) }
end

#set(property_pairs, with_protection = false) ⇒ Object

Accepts a name value pair hash { :name => ‘value’, :pairs => true } and builds a property for each key



84
85
86
87
88
89
90
91
92
93
# File 'lib/property_sets/active_record_extension.rb', line 84

def set(property_pairs, with_protection = false)
  property_pairs.keys.each do |name|
    record = lookup(name)
    if with_protection && record.protected?
      association_class.logger.warn("Someone tried to update the protected #{name} property to #{property_pairs[name]}")
    else
      send("#{name}=", property_pairs[name])
    end
  end
end