Commit 75656f4235737a0c7661e9a98cc5e0b00506ef7d

Authored by AntonioTerceiro
1 parent c3633634

r245@sede: terceiro | 2007-07-29 17:30:24 -0300

ActionItem0: implementing acts_as_design
 


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@249 3f533792-8f58-4932-b0fe-aaf55b0a4547
vendor/plugins/design/init.rb
1 require 'design' 1 require 'design'
  2 +require 'acts_as_design'
2 3
3 class ActionController::Base 4 class ActionController::Base
4 5
@@ -33,19 +34,3 @@ class ActionController::Base @@ -33,19 +34,3 @@ class ActionController::Base
33 34
34 end 35 end
35 36
36 -class ActiveRecord::Base  
37 -  
38 - # declares an ActiveRecord class to be a design. The class is automatically  
39 - # associated with a +has_many+ associationto Design::Block.  
40 - #  
41 - # The underlying database table *must* have a column named +design_data+ of  
42 - # type +text+. +string+ should work too, but you may run into problems  
43 - # related to length limit, so unless you have a very good reason not to, use  
44 - # +text+ type.  
45 - def self.acts_as_design  
46 - has_many :boxes, :class_name => 'Design::Box', :as => :owner  
47 - def blocks  
48 - self.boxes.collect{|b| b.blocks}.flatten  
49 - end  
50 - end  
51 -end  
vendor/plugins/design/lib/acts_as_design.rb 0 → 100644
@@ -0,0 +1,62 @@ @@ -0,0 +1,62 @@
  1 +class ActiveRecord::Base
  2 +
  3 + # declares an ActiveRecord class to be a design. The class is automatically
  4 + # associated with a +has_many+ associationto Design::Block.
  5 + #
  6 + # The underlying database table *must* have a column named +design_data+ of
  7 + # type +text+. +string+ should work too, but you may run into problems
  8 + # related to length limit, so unless you have a very good reason not to, use
  9 + # +text+ type.
  10 + #
  11 + # +acts_as_design+ adds the following methods to your model (besides a
  12 + # +has_many :boxes+ relationship).
  13 + #
  14 + # * template
  15 + # * template=(value)
  16 + # * theme
  17 + # * theme=(value)
  18 + # * icon_theme
  19 + # * icon_theme(value)
  20 + #
  21 + # All these virtual attributes will return <tt>'default'</tt> if set to +nil+
  22 + def self.acts_as_design
  23 + has_many :boxes, :class_name => 'Design::Box', :as => :owner
  24 +
  25 + serialize :design_data
  26 + attr_protected :design_data
  27 +
  28 + def design_data
  29 + self[:design_data] ||= Hash.new
  30 + end
  31 +
  32 + # :nodoc:
  33 + def template
  34 + self.design_data[:template] || 'default'
  35 + end
  36 +
  37 + # :nodoc:
  38 + def template=(value)
  39 + self.design_data[:template] = value
  40 + end
  41 +
  42 + # :nodoc:
  43 + def theme
  44 + self.design_data[:theme] || 'default'
  45 + end
  46 +
  47 + # :nodoc:
  48 + def theme=(value)
  49 + self.design_data[:theme] = value
  50 + end
  51 +
  52 + # :nodoc
  53 + def icon_theme
  54 + self.design_data[:icon_theme] || 'default'
  55 + end
  56 +
  57 + # :nodoc:
  58 + def icon_theme=(value)
  59 + self.design_data[:icon_theme] = value
  60 + end
  61 + end
  62 +end
vendor/plugins/design/test/acts_as_design_test.rb 0 → 100644
@@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
  1 +require File.join(File.dirname(__FILE__), 'test_helper')
  2 +
  3 +class ActsAsDesignTest < Test::Unit::TestCase
  4 +
  5 + def test_should_provide_template_attribute
  6 + user = DesignTestUser.new
  7 + assert_equal 'default', user.template
  8 + user.template = 'other'
  9 + assert_equal 'other', user.template
  10 + user.template = nil
  11 + assert_equal 'default', user.template
  12 + end
  13 +
  14 + def test_should_provide_theme_attribute
  15 + user = DesignTestUser.new
  16 + assert_equal 'default', user.theme
  17 + user.theme = 'other'
  18 + assert_equal 'other', user.theme
  19 + user.theme = nil
  20 + assert_equal 'default', user.theme
  21 + end
  22 +
  23 + def test_should_provide_icon_theme_attribute
  24 + user = DesignTestUser.new
  25 + assert_equal 'default', user.icon_theme
  26 + user.icon_theme = 'other'
  27 + assert_equal 'other', user.icon_theme
  28 + user.icon_theme = nil
  29 + assert_equal 'default', user.icon_theme
  30 + end
  31 +
  32 + def test_should_store_data_in_a_hash
  33 + user = DesignTestUser.new
  34 + assert_kind_of Hash, user.design_data
  35 + end
  36 +
  37 + def test_should_provide_association_with_boxes
  38 + user = DesignTestUser.new
  39 + assert user.boxes << Design::Box.new
  40 + assert_raise ActiveRecord::AssociationTypeMismatch do
  41 + user.boxes << 1
  42 + end
  43 + end
  44 +
  45 +end
vendor/plugins/design/test/schema.rb
@@ -21,6 +21,7 @@ ActiveRecord::Schema.define(:version =&gt; 0) do @@ -21,6 +21,7 @@ ActiveRecord::Schema.define(:version =&gt; 0) do
21 21
22 create_table :design_test_users, :force => true do |t| 22 create_table :design_test_users, :force => true do |t|
23 t.column :name, :string, :limit => 80 23 t.column :name, :string, :limit => 80
  24 + t.column :design_data, :text
24 end 25 end
25 26
26 end 27 end