Commit ad69b0ad05066e38fb4ce669a1b7dcc8a30a3db2

Authored by AntonioTerceiro
1 parent f1ad1b0b

r243@sede: terceiro | 2007-07-29 16:52:19 -0300

ActionItem0: properly testing Design:Helper methods
 


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@247 3f533792-8f58-4932-b0fe-aaf55b0a4547
vendor/plugins/design/lib/design.rb
@@ -26,4 +26,35 @@ module Design @@ -26,4 +26,35 @@ module Design
26 data[:design] # redundant, but makes more clear the return value 26 data[:design] # redundant, but makes more clear the return value
27 end 27 end
28 protected :design 28 protected :design
  29 +
  30 + # returns the path to the designs directory, relative to the +public+
  31 + # directory of your application.
  32 + #
  33 + # Defaults to #{RAILS_ROOT}/public/designs
  34 + def Design.design_root
  35 + Design.instance_variable_get('@design_root') || 'designs'
  36 + end
  37 +
  38 + # sets the path to the designs directory.
  39 + #
  40 + # Passing nil resets +design_root+ to its default value.
  41 + def Design.design_root=(dir)
  42 + Design.instance_variable_set('@design_root', dir)
  43 + end
  44 +
  45 + # :nodoc:
  46 + #
  47 + # used for testing
  48 + def Design.public_filesystem_root
  49 + Design.instance_variable_get('@public_filesystem_root') || File.join(RAILS_ROOT, 'public')
  50 + end
  51 +
  52 + # :nodoc:
  53 + #
  54 + # used for testing
  55 + def Design.public_filesystem_root=(value)
  56 + Design.instance_variable_set('@public_filesystem_root', value)
  57 + end
  58 +
  59 +
29 end 60 end
vendor/plugins/design/lib/design/helper.rb
@@ -76,45 +76,40 @@ module Design @@ -76,45 +76,40 @@ module Design
76 end 76 end
77 end 77 end
78 78
79 - # TODO: test stuff below this comment 79 + ####################################
  80 + # TEMPLATES
  81 + ####################################
80 82
81 - ########################################################  
82 - # Template  
83 - ########################################################  
84 - # Load all the javascript files of a existing template with the template_name passed as argument.  
85 - #  
86 - # The files loaded are in the path: 83 + # Generates <script> tags for all existing javascript files of the current
  84 + # design template.
87 # 85 #
88 - # 'public/templates/#{template_name}/javascripts/*'  
89 - #  
90 - # If a invalid template it's passed the default template is applied  
91 - def javascript_include_tag_for_template  
92 - template_javascript_dir = Dir.glob("#{RAILS_ROOT}/public/templates/#{@ft_config[:template]}/javascripts/*.js") 86 + # The javascript files must be named as *.js and must be under
  87 + # #{RAILS_ROOT}/public/#{Design.design_root}/templates/#{templatename}/javascripts.
  88 + def design_template_javascript_include_tags
  89 + pattern = File.join(Design.public_filesystem_root, Design.design_root, 'templates', design.template, 'javascripts', '*.js')
  90 + javascript_files = Dir.glob(pattern)
93 91
94 - return if template_javascript_dir.blank? 92 + return '' if javascript_files.empty?
95 93
96 - parse_path(template_javascript_dir).map do |filename|  
97 - javascript_include_tag(filename)  
98 - end 94 + javascript_files.map do |filename|
  95 + javascript_include_tag('/' + File.join(Design.design_root, 'templates', design.template, 'javascripts', File.basename(filename)))
  96 + end.join("\n")
99 end 97 end
100 98
101 - # Load all the css files of a existing template with the template_name passed as argument.  
102 - #  
103 - # The files loaded are in the path: 99 + # Generates links to all the CSS files provided by the template being used.
104 # 100 #
105 - # 'public/templates/#{template_name}/stylesheets/*'  
106 - # If a invalid template it's passed the default template is applied  
107 - def stylesheet_link_tag_for_template  
108 - template_stylesheet_dir = Dir.glob("#{RAILS_ROOT}/public/templates/#{@ft_config[:template]}/stylesheets/*.css")  
109 -  
110 - if template_stylesheet_dir.blank?  
111 - flash[:notice] = _("There is no stylesheets in directory %s of template %s.") % [ template_stylesheet_dir, @ft_config[:template]]  
112 - return  
113 - end 101 + # The CSS files must be named as *.css and live in the directory
  102 + # #{RAILS_ROOT}/public/#{Design.design_root}/templates/#{templatename}/stylesheets/
  103 + def design_template_stylesheet_link_tags
114 104
115 - parse_path(template_stylesheet_dir).map do |filename|  
116 - stylesheet_link_tag(filename)  
117 - end 105 + pattern = File.join(Design.public_filesystem_root, Design.design_root, 'templates', design.template, 'stylesheets', '*.css')
  106 + stylesheet_files = Dir.glob(pattern)
  107 +
  108 + return '' if stylesheet_files.empty?
  109 +
  110 + stylesheet_files.map do |filename|
  111 + stylesheet_link_tag('/' + File.join(Design.design_root, 'templates', design.template, 'stylesheets', File.basename(filename)))
  112 + end.join("\n")
118 end 113 end
119 114
120 115
@@ -122,42 +117,37 @@ module Design @@ -122,42 +117,37 @@ module Design
122 #THEMES 117 #THEMES
123 ################################################# 118 #################################################
124 119
125 - # Load all the css files of a existing theme with the @ft_config[:theme] passed as argument in owner object.  
126 - #  
127 - # The files loaded are in the path: 120 + # generates links for all existing theme CSS files in the current design.
128 # 121 #
129 - # 'public/themes/#{theme_name}/*'  
130 - # If a invalid theme it's passed the 'default' theme is applied  
131 - def stylesheet_link_tag_for_theme  
132 - path = "#{RAILS_ROOT}/public/themes/#{@ft_config[:theme]}/"  
133 - theme_dir = Dir.glob(path+"*")  
134 -  
135 - return if theme_dir.blank?  
136 -  
137 - parse_path(theme_dir).map do |filename|  
138 - stylesheet_link_tag(filename)  
139 - end 122 + # The CSS files must be named as *.css and live in the directory
  123 + # #{RAILS_ROOT}/public/#{Design.design_root}/themes/{theme_name}/
  124 + def design_theme_stylesheet_link_tags
  125 + pattern = File.join(Design.public_filesystem_root, Design.design_root, 'themes', design.theme, '*.css')
  126 + stylesheet_files = Dir.glob(pattern)
140 127
141 - end 128 + return '' if stylesheet_files.empty?
142 129
  130 + stylesheet_files.map do |filename|
  131 + stylesheet_link_tag('/' + File.join(Design.design_root, 'themes', design.theme, File.basename(filename)))
  132 + end.join("\n")
143 133
144 - #Display a given icon passed as argument  
145 - #The icon path should be '/icons/{icon_theme}/{icon_image}'  
146 - def display_icon(icon, options = {})  
147 - image_tag("/icons/#{@ft_config[:icon_theme]}/#{icon}.png", options)  
148 end 134 end
149 135
150 - private  
151 - 136 + ###############################################
  137 + # ICON THEME STUFF
  138 + ###############################################
152 139
153 - # Check if the current controller is the controller that allows layout editing  
154 - def edit_mode?  
155 - controller.flexible_template_edit_template?  
156 - end  
157 -  
158 - def parse_path(files_path = [], remove_until = 'public')  
159 - remove_until = remove_until.gsub(/\//, '\/')  
160 - files_path.map{|f| f.gsub(/.*#{remove_until}/, '')} 140 + # displays the icon given named after the +icon+ argument, using the
  141 + # current icon_theme.
  142 + #
  143 + # There must be a file named +icon+.png in the directory
  144 + # #{RAILS_ROOT}/public/designs/icons/#{icon_theme}
  145 + #
  146 + # Ths optional +options+ argument is passed untouched to the image_tag
  147 + # Rails helper
  148 + def design_display_icon(icon, options = {})
  149 + filename = (icon =~ /\.png$/) ? icon : (icon + '.png')
  150 + image_tag('/' + File.join(Design.design_root, 'icons', design.icon_theme, filename), options)
161 end 151 end
162 152
163 end # END OF module Helper 153 end # END OF module Helper
vendor/plugins/design/test/design_helper_test.rb
@@ -38,6 +38,27 @@ class DesignHelperTestController &lt; ActionController::Base @@ -38,6 +38,27 @@ class DesignHelperTestController &lt; ActionController::Base
38 def index 38 def index
39 render :inline => '<%= design_display("my content") %>' 39 render :inline => '<%= design_display("my content") %>'
40 end 40 end
  41 +
  42 + def javascript
  43 + render :inline => '<%= design_template_javascript_include_tags %>'
  44 + end
  45 +
  46 + def template_stylesheets
  47 + render :inline => '<%= design_template_stylesheet_link_tags %>'
  48 + end
  49 +
  50 + def theme_stylesheets
  51 + render :inline => '<%= design_theme_stylesheet_link_tags %>'
  52 + end
  53 +
  54 + def icons
  55 + # one with extension and other without
  56 + render :inline => '
  57 + <%= design_display_icon("something") %>
  58 + <%= design_display_icon("another.png") %>
  59 + '
  60 + end
  61 +
41 end 62 end
42 63
43 class DesignHelperTest < Test::Unit::TestCase 64 class DesignHelperTest < Test::Unit::TestCase
@@ -46,6 +67,12 @@ class DesignHelperTest &lt; Test::Unit::TestCase @@ -46,6 +67,12 @@ class DesignHelperTest &lt; Test::Unit::TestCase
46 @controller = DesignHelperTestController.new 67 @controller = DesignHelperTestController.new
47 @request = ActionController::TestRequest.new 68 @request = ActionController::TestRequest.new
48 @response = ActionController::TestResponse.new 69 @response = ActionController::TestResponse.new
  70 +
  71 + Design.public_filesystem_root = File.join(File.dirname(__FILE__))
  72 + end
  73 +
  74 + def teardown
  75 + Design.public_filesystem_root = nil
49 end 76 end
50 77
51 def test_should_generate_all_boxes 78 def test_should_generate_all_boxes
@@ -78,4 +105,46 @@ class DesignHelperTest &lt; Test::Unit::TestCase @@ -78,4 +105,46 @@ class DesignHelperTest &lt; Test::Unit::TestCase
78 assert_tag :tag => 'div', :content => "this is a fixed content block hacked for testing", :attributes => { :class => 'block' } 105 assert_tag :tag => 'div', :content => "this is a fixed content block hacked for testing", :attributes => { :class => 'block' }
79 end 106 end
80 107
  108 + def test_should_provide_javascript_link_if_available
  109 + get :javascript
  110 + assert_tag :tag => 'script', :attributes => {
  111 + :type => 'text/javascript',
  112 + :src => '/designs/templates/default/javascripts/one.js'
  113 + }
  114 + assert_tag :tag => 'script', :attributes => {
  115 + :type => 'text/javascript',
  116 + :src => '/designs/templates/default/javascripts/two.js'
  117 + }
  118 + end
  119 +
  120 + def test_should_provide_stylesheet_links_if_available
  121 + get :template_stylesheets
  122 + assert_tag :tag => 'link', :attributes => {
  123 + :type => 'text/css',
  124 + :href => '/designs/templates/default/stylesheets/one.css'
  125 + }
  126 + assert_tag :tag => 'link', :attributes => {
  127 + :type => 'text/css',
  128 + :href => '/designs/templates/default/stylesheets/two.css'
  129 + }
  130 + end
  131 +
  132 + def test_should_provide_theme_stylesheet_links_if_available
  133 + get :theme_stylesheets
  134 + assert_tag :tag => 'link', :attributes => {
  135 + :type => 'text/css',
  136 + :href => '/designs/themes/default/one.css'
  137 + }
  138 + end
  139 +
  140 + def test_should_support_displaying_icons
  141 + get :icons
  142 + assert_tag :tag => 'img', :attributes => {
  143 + :src => '/designs/icons/default/something.png'
  144 + }
  145 + assert_tag :tag => 'img', :attributes => {
  146 + :src => '/designs/icons/default/another.png'
  147 + }
  148 + end
  149 +
81 end 150 end
vendor/plugins/design/test/design_test.rb
@@ -31,4 +31,23 @@ class DesignTest &lt; Test::Unit::TestCase @@ -31,4 +31,23 @@ class DesignTest &lt; Test::Unit::TestCase
31 assert_kind_of Hash, FixedDesignTestController.design_plugin_config 31 assert_kind_of Hash, FixedDesignTestController.design_plugin_config
32 end 32 end
33 33
  34 + def test_should_be_able_to_change_design_root
  35 + Design.design_root = 'some_design_root'
  36 + assert_equal 'some_design_root', Design.design_root
  37 + Design.design_root = nil # reset
  38 + end
  39 +
  40 + def test_should_provide_a_sensible_default_for_design_root
  41 + Design.design_root = nil
  42 + assert_equal File.join('designs'), Design.design_root
  43 + end
  44 +
  45 + # used for testing
  46 + def test_changing_public_filesystem_root
  47 + Design.public_filesystem_root = 'test'
  48 + assert_equal 'test', Design.public_filesystem_root
  49 + Design.public_filesystem_root = nil
  50 + assert_equal File.join(RAILS_ROOT, 'public'), Design.public_filesystem_root
  51 + end
  52 +
34 end 53 end
vendor/plugins/design/test/designs/icons/default/another.png 0 → 100644
vendor/plugins/design/test/designs/icons/default/something.png 0 → 100644
vendor/plugins/design/test/designs/themes/default/one.css 0 → 100644
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +/* empty */