Commit 00d53e2f71d97aced0c96d98865cf31f13505170

Authored by AntonioTerceiro
1 parent c247ec24

ActionItem41: moving serialized settings Hash logic to its own module


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1316 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/block.rb
@@ -7,21 +7,7 @@ class Block < ActiveRecord::Base @@ -7,21 +7,7 @@ class Block < ActiveRecord::Base
7 acts_as_list :scope => :box 7 acts_as_list :scope => :box
8 belongs_to :box 8 belongs_to :box
9 9
10 - serialize :settings, Hash  
11 - def settings  
12 - self[:settings] ||= Hash.new  
13 - end  
14 -  
15 - def self.settings_item(name)  
16 - class_eval <<-CODE  
17 - def #{name}  
18 - settings[:#{name}]  
19 - end  
20 - def #{name}=(value)  
21 - settings[:#{name}] = value  
22 - end  
23 - CODE  
24 - end 10 + acts_as_having_settings
25 11
26 def self.description 12 def self.description
27 _('A dummy block.') 13 _('A dummy block.')
app/models/recent_documents_block.rb
@@ -4,7 +4,7 @@ class RecentDocumentsBlock &lt; Block @@ -4,7 +4,7 @@ class RecentDocumentsBlock &lt; Block
4 _('List of recent content') 4 _('List of recent content')
5 end 5 end
6 6
7 - settings_item :limit 7 + settings_items :limit
8 8
9 def content 9 def content
10 docs = 10 docs =
lib/acts_as_having_settings.rb 0 → 100644
@@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
  1 +module ActsAsHavingSettings
  2 +
  3 + module ClassMethods
  4 + def acts_as_having_settings(*args)
  5 + options = args.last.is_a?(Hash) ? args.pop : {}
  6 +
  7 + settings_field = options[:field] || 'settings'
  8 +
  9 + class_eval <<-CODE
  10 + serialize :#{settings_field}, Hash
  11 + def self.settings_field
  12 + #{settings_field.inspect}
  13 + end
  14 + def #{settings_field}
  15 + self[:#{settings_field}] ||= Hash.new
  16 + end
  17 + CODE
  18 + settings_items(*args)
  19 + end
  20 +
  21 + def settings_items(*names)
  22 + names.each do |setting|
  23 + class_eval <<-CODE
  24 + def #{setting}
  25 + send(self.class.settings_field)[:#{setting}]
  26 + end
  27 + def #{setting}=(value)
  28 + send(self.class.settings_field)[:#{setting}] = value
  29 + end
  30 + CODE
  31 + end
  32 + end
  33 +
  34 + end
  35 +
  36 +end
  37 +
  38 +ActiveRecord::Base.send(:extend, ActsAsHavingSettings::ClassMethods)
test/unit/acts_as_having_settings_test.rb 0 → 100644
@@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class ActsAsHavingSettingsTest < Test::Unit::TestCase
  4 +
  5 + # using Block class as a sample user of the module
  6 +
  7 + should 'store settings in a hash' do
  8 + block = Block.new
  9 +
  10 + assert_kind_of Hash, block.settings
  11 + block.save!
  12 +
  13 + assert_kind_of Hash, Block.find(block.id).settings
  14 + end
  15 +
  16 + should 'be able to declare settings items' do
  17 + block_class = Class.new(Block)
  18 +
  19 + block = block_class.new
  20 + assert !block.respond_to?(:limit)
  21 + assert !block.respond_to?(:limit=)
  22 +
  23 + block_class.settings_items :limit
  24 +
  25 + assert_respond_to block, :limit
  26 + assert_respond_to block, :limit=
  27 +
  28 + assert_nil block.limit
  29 + block.limit = 10
  30 + assert_equal 10, block.limit
  31 +
  32 + assert_equal({ :limit => 10}, block.settings)
  33 + end
  34 +
  35 + should 'properly save the settings' do
  36 + # RecentDocumentsBlock declares an actual setting called limit
  37 + profile = create_user('testuser').person
  38 + block = RecentDocumentsBlock.new
  39 + block.box = profile.boxes.first
  40 + block.limit = 15
  41 + block.save!
  42 + assert_equal 15, Block.find(block.id).limit
  43 + end
  44 +
  45 +end
test/unit/block_test.rb
@@ -5,16 +5,6 @@ class BlockTest &lt; Test::Unit::TestCase @@ -5,16 +5,6 @@ class BlockTest &lt; Test::Unit::TestCase
5 should 'describe itself' do 5 should 'describe itself' do
6 assert_kind_of String, Block.description 6 assert_kind_of String, Block.description
7 end 7 end
8 -  
9 - should 'store settings in a hash' do  
10 - block = Block.new  
11 -  
12 - assert_kind_of Hash, block.settings  
13 - block.save!  
14 -  
15 - assert_kind_of Hash, Block.find(block.id).settings  
16 - end  
17 -  
18 8
19 should 'access owner through box' do 9 should 'access owner through box' do
20 user = create_user('testinguser').person 10 user = create_user('testinguser').person
@@ -32,25 +22,6 @@ class BlockTest &lt; Test::Unit::TestCase @@ -32,25 +22,6 @@ class BlockTest &lt; Test::Unit::TestCase
32 assert_nil Block.new.owner 22 assert_nil Block.new.owner
33 end 23 end
34 24
35 - should 'be able to declare settings items' do  
36 - block_class = Class.new(Block)  
37 -  
38 - block = block_class.new  
39 - assert !block.respond_to?(:limit)  
40 - assert !block.respond_to?(:limit=)  
41 -  
42 - block_class.settings_item :limit  
43 -  
44 - assert_respond_to block, :limit  
45 - assert_respond_to block, :limit=  
46 -  
47 - assert_nil block.limit  
48 - block.limit = 10  
49 - assert_equal 10, block.limit  
50 -  
51 - assert_equal({ :limit => 10}, block.settings)  
52 - end  
53 -  
54 should 'generate CSS class name' do 25 should 'generate CSS class name' do
55 block = Block.new 26 block = Block.new
56 block.class.expects(:name).returns('SomethingBlock') 27 block.class.expects(:name).returns('SomethingBlock')