require 'test_helper' class ActsAsPreferencedFieldTest < ActiveSupport::TestCase class User < FakeModel attr_accessor :hold_preferences_here acts_as_preferenced( :field => :hold_preferences_here ) do |config| config.preference :items_per_page, :section => :general, :choices => (1..100), :default => 20 config.section :likes, :prefix => true do |section| section.preference :flowers, :default => true section.preference :animals end config.preference :likes_people, :choices => [:no, :little, :yes] end end def setup @user = User.new end test "all preferences" do all = User.all_preferences assert_equal [:items_per_page, :likes_flowers, :likes_animals, :likes_people], all.collect(&:name) end test "preference sections" do assert_equal [:general, nil, :likes], User.preference_sections prefs = User.preferences_for_section( :sdjkfajsdf ) assert prefs.empty? prefs = User.preferences_for_section( :likes ) assert_equal [:likes_flowers, :likes_animals], prefs.collect(&:name) prefs = User.preferences_for_section( nil ) assert_equal [:likes_people], prefs.collect(&:name) end test "defaults" do assert_equal true, @user.likes_flowers? assert_equal 20, @user.items_per_page assert_nil @user.likes_animals? assert_nil @user.likes_people end test "set boolean" do @user.likes_flowers = false assert_equal false, @user.likes_flowers? end test "set integer" do @user.items_per_page = 11 assert_equal 11, @user.items_per_page end test "set enumerable" do @user.likes_people = :little assert_equal :little, @user.likes_people end test "set integer as boolean" do assert_raises( ArgumentError ) do @user.items_per_page = true end end test "set integer out of range" do assert_raises( ArgumentError ) do @user.items_per_page = 0 end assert_raises( ArgumentError ) do @user.items_per_page = 101 end end test "set boolean as integer" do assert_raises( ArgumentError ) do @user.likes_flowers = 10 end end test "set enumerable out of range" do assert_raises( ArgumentError ) do @user.likes_people = :lot end end test "set enumerable as string" do @user.likes_people = "yes" assert_equal :yes, @user.likes_people end test "set batch preferences" do @user.likes_flowers = true @user.likes_animals = true @user.preferences = { :items_per_page => 23 } assert_equal 23, @user.items_per_page # No value for boolean defaulting to true - we assume it was unset # TODO: in fact Rails handle this by sending hidden field with the same # name, use the same approach assert_equal false, @user.likes_flowers? # No value for this boolean without default - reset it # TODO: this also doesn't seem correct assert_nil @user.likes_animals? end end class ActsAsPreferencedAssociationTest < ActiveSupport::TestCase class UserPreference < FakeModel # Tons of fakes here attr_accessor :attributes def items_per_page attributes[:items_per_page] end def items_per_page= (value) attributes[:items_per_page] = value end def likes_flowers? attributes[:likes_flowers] end def likes_flowers= (value) attributes[:likes_flowers] = value end def likes_animals? attributes[:likes_animals] end def likes_animals= (value) attributes[:likes_animals] = value end def likes_people? attributes[:likes_people] end def likes_people= (value) attributes[:likes_people] = value end alias_method :likes_flowers, :likes_flowers? alias_method :likes_animals, :likes_animals? alias_method :likes_people, :likes_people? delegate :[], :[]=, :to => :attributes end class User < FakeModel attr_accessor :user_preferences acts_as_preferenced( :association => :user_preferences ) do |config| config.preference :items_per_page, :section => :general, :choices => (1..100), :default => 20 config.section :likes, :prefix => true do |section| section.preference :flowers, :default => true section.preference :animals end config.preference :likes_people, :choices => [:no, :little, :yes] end end def setup @user = User.new @user.user_preferences = UserPreference.new end test "all preferences" do all = User.all_preferences assert_equal [:items_per_page, :likes_flowers, :likes_animals, :likes_people], all.collect(&:name) end test "preference sections" do assert_equal [:general, nil, :likes], User.preference_sections prefs = User.preferences_for_section( :sdjkfajsdf ) assert prefs.empty? prefs = User.preferences_for_section( :likes ) assert_equal [:likes_flowers, :likes_animals], prefs.collect(&:name) prefs = User.preferences_for_section( nil ) assert_equal [:likes_people], prefs.collect(&:name) end test "defaults" do assert_equal true, @user.likes_flowers? assert_equal 20, @user.items_per_page assert_nil @user.likes_animals? assert_nil @user.likes_people end test "set boolean" do @user.likes_flowers = false assert_equal false, @user.likes_flowers? end test "set integer" do @user.items_per_page = 11 assert_equal 11, @user.items_per_page end test "set enumerable" do @user.likes_people = :little assert_equal :little, @user.likes_people end test "set integer as boolean" do assert_raises( ArgumentError ) do @user.items_per_page = true end end test "set integer out of range" do assert_raises( ArgumentError ) do @user.items_per_page = 0 end assert_raises( ArgumentError ) do @user.items_per_page = 101 end end test "set boolean as integer" do assert_raises( ArgumentError ) do @user.likes_flowers = 10 end end test "set enumerable out of range" do assert_raises( ArgumentError ) do @user.likes_people = :lot end end test "set enumerable as string" do @user.likes_people = "yes" assert_equal :yes, @user.likes_people end test "set batch preferences" do @user.likes_flowers = true @user.likes_animals = false @user.preferences = { :items_per_page => 23 } assert_equal 23, @user.items_per_page # No value for boolean defaulting to true - we assume it was unset # TODO: in fact Rails handle this by sending hidden field with the same # name, use the same approach assert_equal false, @user.likes_flowers? # No value for this boolean without default - reset it # TODO: this also doesn't seem correct assert_nil @user.likes_animals? end end