From 75656f4235737a0c7661e9a98cc5e0b00506ef7d Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Tue, 31 Jul 2007 13:08:44 +0000 Subject: [PATCH] r245@sede: terceiro | 2007-07-29 17:30:24 -0300 ActionItem0: implementing acts_as_design --- vendor/plugins/design/init.rb | 17 +---------------- vendor/plugins/design/lib/acts_as_design.rb | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vendor/plugins/design/test/acts_as_design_test.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ vendor/plugins/design/test/schema.rb | 1 + 4 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 vendor/plugins/design/lib/acts_as_design.rb create mode 100644 vendor/plugins/design/test/acts_as_design_test.rb diff --git a/vendor/plugins/design/init.rb b/vendor/plugins/design/init.rb index 78c761b..aba8d30 100644 --- a/vendor/plugins/design/init.rb +++ b/vendor/plugins/design/init.rb @@ -1,4 +1,5 @@ require 'design' +require 'acts_as_design' class ActionController::Base @@ -33,19 +34,3 @@ class ActionController::Base end -class ActiveRecord::Base - - # declares an ActiveRecord class to be a design. The class is automatically - # associated with a +has_many+ associationto Design::Block. - # - # The underlying database table *must* have a column named +design_data+ of - # type +text+. +string+ should work too, but you may run into problems - # related to length limit, so unless you have a very good reason not to, use - # +text+ type. - def self.acts_as_design - has_many :boxes, :class_name => 'Design::Box', :as => :owner - def blocks - self.boxes.collect{|b| b.blocks}.flatten - end - end -end diff --git a/vendor/plugins/design/lib/acts_as_design.rb b/vendor/plugins/design/lib/acts_as_design.rb new file mode 100644 index 0000000..19099fa --- /dev/null +++ b/vendor/plugins/design/lib/acts_as_design.rb @@ -0,0 +1,62 @@ +class ActiveRecord::Base + + # declares an ActiveRecord class to be a design. The class is automatically + # associated with a +has_many+ associationto Design::Block. + # + # The underlying database table *must* have a column named +design_data+ of + # type +text+. +string+ should work too, but you may run into problems + # related to length limit, so unless you have a very good reason not to, use + # +text+ type. + # + # +acts_as_design+ adds the following methods to your model (besides a + # +has_many :boxes+ relationship). + # + # * template + # * template=(value) + # * theme + # * theme=(value) + # * icon_theme + # * icon_theme(value) + # + # All these virtual attributes will return 'default' if set to +nil+ + def self.acts_as_design + has_many :boxes, :class_name => 'Design::Box', :as => :owner + + serialize :design_data + attr_protected :design_data + + def design_data + self[:design_data] ||= Hash.new + end + + # :nodoc: + def template + self.design_data[:template] || 'default' + end + + # :nodoc: + def template=(value) + self.design_data[:template] = value + end + + # :nodoc: + def theme + self.design_data[:theme] || 'default' + end + + # :nodoc: + def theme=(value) + self.design_data[:theme] = value + end + + # :nodoc + def icon_theme + self.design_data[:icon_theme] || 'default' + end + + # :nodoc: + def icon_theme=(value) + self.design_data[:icon_theme] = value + end + end +end diff --git a/vendor/plugins/design/test/acts_as_design_test.rb b/vendor/plugins/design/test/acts_as_design_test.rb new file mode 100644 index 0000000..1aa95a0 --- /dev/null +++ b/vendor/plugins/design/test/acts_as_design_test.rb @@ -0,0 +1,45 @@ +require File.join(File.dirname(__FILE__), 'test_helper') + +class ActsAsDesignTest < Test::Unit::TestCase + + def test_should_provide_template_attribute + user = DesignTestUser.new + assert_equal 'default', user.template + user.template = 'other' + assert_equal 'other', user.template + user.template = nil + assert_equal 'default', user.template + end + + def test_should_provide_theme_attribute + user = DesignTestUser.new + assert_equal 'default', user.theme + user.theme = 'other' + assert_equal 'other', user.theme + user.theme = nil + assert_equal 'default', user.theme + end + + def test_should_provide_icon_theme_attribute + user = DesignTestUser.new + assert_equal 'default', user.icon_theme + user.icon_theme = 'other' + assert_equal 'other', user.icon_theme + user.icon_theme = nil + assert_equal 'default', user.icon_theme + end + + def test_should_store_data_in_a_hash + user = DesignTestUser.new + assert_kind_of Hash, user.design_data + end + + def test_should_provide_association_with_boxes + user = DesignTestUser.new + assert user.boxes << Design::Box.new + assert_raise ActiveRecord::AssociationTypeMismatch do + user.boxes << 1 + end + end + +end diff --git a/vendor/plugins/design/test/schema.rb b/vendor/plugins/design/test/schema.rb index d9ade39..fb1678c 100644 --- a/vendor/plugins/design/test/schema.rb +++ b/vendor/plugins/design/test/schema.rb @@ -21,6 +21,7 @@ ActiveRecord::Schema.define(:version => 0) do create_table :design_test_users, :force => true do |t| t.column :name, :string, :limit => 80 + t.column :design_data, :text end end -- libgit2 0.21.2