Module: Spree::Preferences::ModelHooks::InstanceMethods
- Defined in:
- lib/spree_core/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(name, group = nil) ⇒ Object
Gets the preferred value for the given attribute.
-
#prefers?(name, group = nil) ⇒ Boolean
Queries whether or not a value has been specified for the given attribute.
-
#set_preference(name, value, group = nil) ⇒ Object
Sets a new value for the given attribute.
Class Method Details
.included(base) ⇒ Object
:nodoc:
143 144 145 146 147 |
# File 'lib/spree_core/preferences/model_hooks.rb', line 143 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"}
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/spree_core/preferences/model_hooks.rb', line 172 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.where(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.name] = preference.value all_preferences end end |
#preferred(name, 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'
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/spree_core/preferences/model_hooks.rb', line 228 def preferred(name, group = nil) name = name.to_s if @preference_values && @preference_values[name] && @preference_values[name].include?(group) value = @preference_values[name][group] else group_id, group_type = Preference.split_group(group) preference = stored_preferences.find(:first, :conditions => {:name => name, :group_id => group_id, :group_type => group_type}) value = preference ? preference.value : preference_definitions[name].default_value end value end |
#prefers?(name, 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
210 211 212 213 214 215 |
# File 'lib/spree_core/preferences/model_hooks.rb', line 210 def prefers?(name, group = nil) name = name.to_s value = preferred(name, group) preference_definitions[name].query(value) end |
#set_preference(name, 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!
254 255 256 257 258 259 260 261 262 |
# File 'lib/spree_core/preferences/model_hooks.rb', line 254 def set_preference(name, value, group = nil) name = name.to_s @preference_values ||= {} @preference_values[name] ||= {} @preference_values[name][group] = value value end |