From 00d53e2f71d97aced0c96d98865cf31f13505170 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Fri, 8 Feb 2008 23:35:25 +0000 Subject: [PATCH] ActionItem41: moving serialized settings Hash logic to its own module --- app/models/block.rb | 16 +--------------- app/models/recent_documents_block.rb | 2 +- lib/acts_as_having_settings.rb | 38 ++++++++++++++++++++++++++++++++++++++ test/unit/acts_as_having_settings_test.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/unit/block_test.rb | 29 ----------------------------- 5 files changed, 85 insertions(+), 45 deletions(-) create mode 100644 lib/acts_as_having_settings.rb create mode 100644 test/unit/acts_as_having_settings_test.rb diff --git a/app/models/block.rb b/app/models/block.rb index 92ac6ae..1356df8 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -7,21 +7,7 @@ class Block < ActiveRecord::Base acts_as_list :scope => :box belongs_to :box - serialize :settings, Hash - def settings - self[:settings] ||= Hash.new - end - - def self.settings_item(name) - class_eval <<-CODE - def #{name} - settings[:#{name}] - end - def #{name}=(value) - settings[:#{name}] = value - end - CODE - end + acts_as_having_settings def self.description _('A dummy block.') diff --git a/app/models/recent_documents_block.rb b/app/models/recent_documents_block.rb index b02c72a..f6946ec 100644 --- a/app/models/recent_documents_block.rb +++ b/app/models/recent_documents_block.rb @@ -4,7 +4,7 @@ class RecentDocumentsBlock < Block _('List of recent content') end - settings_item :limit + settings_items :limit def content docs = diff --git a/lib/acts_as_having_settings.rb b/lib/acts_as_having_settings.rb new file mode 100644 index 0000000..d6941ca --- /dev/null +++ b/lib/acts_as_having_settings.rb @@ -0,0 +1,38 @@ +module ActsAsHavingSettings + + module ClassMethods + def acts_as_having_settings(*args) + options = args.last.is_a?(Hash) ? args.pop : {} + + settings_field = options[:field] || 'settings' + + class_eval <<-CODE + serialize :#{settings_field}, Hash + def self.settings_field + #{settings_field.inspect} + end + def #{settings_field} + self[:#{settings_field}] ||= Hash.new + end + CODE + settings_items(*args) + end + + def settings_items(*names) + names.each do |setting| + class_eval <<-CODE + def #{setting} + send(self.class.settings_field)[:#{setting}] + end + def #{setting}=(value) + send(self.class.settings_field)[:#{setting}] = value + end + CODE + end + end + + end + +end + +ActiveRecord::Base.send(:extend, ActsAsHavingSettings::ClassMethods) diff --git a/test/unit/acts_as_having_settings_test.rb b/test/unit/acts_as_having_settings_test.rb new file mode 100644 index 0000000..05aeecb --- /dev/null +++ b/test/unit/acts_as_having_settings_test.rb @@ -0,0 +1,45 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ActsAsHavingSettingsTest < Test::Unit::TestCase + + # using Block class as a sample user of the module + + 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 + +end diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb index 7d1dd52..3dd80df 100644 --- a/test/unit/block_test.rb +++ b/test/unit/block_test.rb @@ -5,16 +5,6 @@ class BlockTest < Test::Unit::TestCase should 'describe itself' do assert_kind_of String, Block.description 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 'access owner through box' do user = create_user('testinguser').person @@ -32,25 +22,6 @@ class BlockTest < Test::Unit::TestCase assert_nil Block.new.owner 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_item :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 'generate CSS class name' do block = Block.new block.class.expects(:name).returns('SomethingBlock') -- libgit2 0.21.2