lib/user_preferences/api.rb in user_preferences-0.0.3 vs lib/user_preferences/api.rb in user_preferences-1.0.0
- old
+ new
@@ -16,10 +16,11 @@
def set(hash)
hash_setter do
hash.each do |name, value|
find_or_init_preference(name).update_value!(value)
end
+ reload
end
end
def reload
@_saved_preferences = nil
@@ -39,14 +40,10 @@
def saved_preferences
@_saved_preferences ||= @scope.select([:id, :category, :name, :value, :user_id])
end
def find_or_init_preference(name)
- unless preference = saved_preferences.detect { |p| p.name == name }
- preference = @scope.find_by_name(name) || @scope.build(name: name, category: @category)
- saved_preferences << preference
- end
- preference
+ @scope.find_or_initialize_by(name: name, category: @category)
end
def hash_setter(&block)
ActiveRecord::Base.transaction do
result = true