acts_as_having_settings_test.rb
2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require File.dirname(__FILE__) + '/../test_helper'
class ActsAsHavingSettingsTest < ActiveSupport::TestCase
  # using Block class as a sample user of the module 
  class TestClass < Block
    settings_items :flag, :type => :boolean
    settings_items :flag_disabled_by_default, :type => :boolean, :default => false
    settings_items :name, :type => :string, :default => N_('ENGLISH TEXT')
  end
  should 'store settings in a hash' do
    block = Block.new
    assert_kind_of Hash, block.settings
    block.save!
    assert_kind_of Hash, Block.find(block.id).settings
  end
  should 'be able to declare settings items' do
    block_class = Class.new(Block)
    block = block_class.new
    assert !block.respond_to?(:limit)
    assert !block.respond_to?(:limit=)
    block_class.settings_items :limit
    assert_respond_to block, :limit
    assert_respond_to block, :limit=
    assert_nil block.limit
    block.limit = 10
    assert_equal 10, block.limit
    assert_equal({ :limit => 10}, block.settings)
  end
  should 'properly save the settings' do
    # RecentDocumentsBlock declares an actual setting called limit
    profile = create_user('testuser').person
    block = RecentDocumentsBlock.new
    block.box = profile.boxes.first
    block.limit = 15
    block.save!
    assert_equal 15, Block.find(block.id).limit
  end
  should 'be able to specify default values' do
    block_class = Class.new(Block)
    block_class.settings_items :some_setting, :default => 10
    assert_equal 10, block_class.new.some_setting
  end
  should 'be able to set boolean attributes to false with a default of true' do
    obj = TestClass.new
    obj.flag = false
    assert_equal false, obj.flag
  end
  should 'return false by default when the default is false' do
    assert_equal false, TestClass.new.flag_disabled_by_default
  end
  should 'translate default values' do
    TestClass.any_instance.expects(:gettext).with('ENGLISH TEXT').returns("TRANSLATED")
    assert_equal 'TRANSLATED', TestClass.new.name
  end
  should 'be able to specify type of atrributes (boolean)' do
    obj = TestClass.new
    obj.flag = 'true'
    assert_equal true, obj.flag
  end
  should 'symbolize keys when save' do
    obj = TestClass.new
    obj.settings.expects(:symbolize_keys!).once
    assert obj.save
  end
end