Module: Spree::Preferences::ModelHooks::InstanceMethods
- Defined in:
- lib/spree/preferences/model_hooks.rb
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#preferences(*args) ⇒ Object
Finds all preferences, including defaults, for the current record.
-
#preferred(attribute, group = nil) ⇒ Object
Gets the preferred value for the given attribute.
-
#prefers?(attribute, group = nil) ⇒ Boolean
Queries whether or not a value has been specified for the given attribute.
-
#set_preference(attribute, value, group = nil) ⇒ Object
Sets a new value for the given attribute.
Class Method Details
.included(base) ⇒ Object
:nodoc:
145 146 147 148 149 |
# File 'lib/spree/preferences/model_hooks.rb', line 145 def self.included(base) #:nodoc: base.class_eval do alias_method :prefs, :preferences end end |
Instance Method Details
#preferences(*args) ⇒ Object
Finds all preferences, including defaults, for the current record. If any custom group preferences have been stored, then this will include all default preferences within that particular group.
Examples
A user with no stored values:
user = User.find(:first)
user.preferences
=> {"language"=>"English", "color"=>nil}
A user with stored values for a particular group:
user.preferred_color = 'red', 'cars'
user.preferences
=> {"language"=>"English", "color"=>nil, "cars"=>{"language=>"English", "color"=>"red"}}
Getting preference values for the owning record:
user.preferences(nil)
=> {"language"=>"English", "color"=>nil}
Getting preference values for a particular group:
user.preferences('cars')
=> {"language"=>"English", "color"=>"red"}
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/spree/preferences/model_hooks.rb', line 174 def preferences(*args) if args.empty? group = nil conditions = {} else group = args.first group_id, group_type = Preference.split_group(group) conditions = {:group_id => group_id, :group_type => group_type} end # Find all of the stored preferences stored_preferences = self.stored_preferences.find(:all, :conditions => conditions) # Hashify attribute -> value or group -> attribute -> value stored_preferences.inject(self.class.default_preferences.dup) do |all_preferences, preference| if !group && (preference_group = preference.group) preferences = all_preferences[preference_group] ||= self.class.default_preferences.dup else preferences = all_preferences end preferences[preference.attribute] = preference.value all_preferences end end |
#preferred(attribute, group = nil) ⇒ Object
Gets the preferred value for the given attribute.
Examples
user = User.find(:first)
user.preferred(:color) # => 'red'
user.preferred(:color, 'cars') # => 'blue'
car = Car.find(:first)
user.preferred(:color, car) # => 'black'
230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/spree/preferences/model_hooks.rb', line 230 def preferred(attribute, group = nil) attribute = attribute.to_s if @preference_values && @preference_values[attribute] && @preference_values[attribute].include?(group) value = @preference_values[attribute][group] else group_id, group_type = Preference.split_group(group) preference = stored_preferences.find(:first, :conditions => {:attribute => attribute, :group_id => group_id, :group_type => group_type}) value = preference ? preference.value : preference_definitions[attribute].default_value end value end |
#prefers?(attribute, group = nil) ⇒ Boolean
Queries whether or not a value has been specified for the given attribute. This is dependent on how the value is type-casted.
Examples
user = User.find(:first)
user.prefers?(:notifications) # => true
user.prefers(:notifications, 'error') # => true
newsgroup = Newsgroup.find(:first)
user.prefers?(:notifications, newsgroup) # => false
212 213 214 215 216 217 |
# File 'lib/spree/preferences/model_hooks.rb', line 212 def prefers?(attribute, group = nil) attribute = attribute.to_s value = preferred(attribute, group) preference_definitions[attribute].query(value) end |
#set_preference(attribute, value, group = nil) ⇒ Object
Sets a new value for the given attribute. The actual Preference record is not created until the actual record is saved.
Examples
user = User.find(:first)
user.set_preference(:notifications, false) # => false
user.save!
newsgroup = Newsgroup.find(:first)
user.set_preference(:notifications, true, newsgroup) # => true
user.save!
256 257 258 259 260 261 262 263 264 |
# File 'lib/spree/preferences/model_hooks.rb', line 256 def set_preference(attribute, value, group = nil) attribute = attribute.to_s @preference_values ||= {} @preference_values[attribute] ||= {} @preference_values[attribute][group] = value value end |