sections_rails 0.6.10 → 0.6.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/lib/sections_rails/section.rb +60 -30
  2. data/lib/sections_rails/version.rb +1 -1
  3. data/spec/controllers/errors_controller_spec.rb +15 -0
  4. data/spec/controllers/partials_controller_spec.rb +74 -0
  5. data/spec/controllers/script_assets_controller_spec.rb +68 -0
  6. data/spec/controllers/style_assets_controller_spec.rb +82 -0
  7. data/spec/controllers/view_types_controller_spec.rb +22 -0
  8. data/spec/dummy/app/assets/images/rails.png +0 -0
  9. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  10. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  11. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  12. data/spec/dummy/app/controllers/errors_controller.rb +5 -0
  13. data/spec/dummy/app/controllers/partials_controller.rb +20 -0
  14. data/spec/dummy/app/controllers/script_assets_controller.rb +18 -0
  15. data/spec/dummy/app/controllers/style_assets_controller.rb +27 -0
  16. data/spec/dummy/app/controllers/view_types_controller.rb +10 -0
  17. data/spec/dummy/app/views/errors/missing_section.html.erb +1 -0
  18. data/spec/dummy/app/views/layouts/application.html.erb +1 -0
  19. data/spec/dummy/app/views/partials/custom_partial.html.erb +1 -0
  20. data/spec/dummy/app/views/partials/disabled.html.erb +2 -0
  21. data/spec/dummy/app/views/partials/erb_section.html.erb +1 -0
  22. data/spec/dummy/app/views/partials/haml_section.html.erb +1 -0
  23. data/spec/dummy/app/views/partials/no_options.html.erb +1 -0
  24. data/spec/dummy/app/views/partials/production_mode.html.erb +1 -0
  25. data/spec/dummy/app/views/script_assets/coffeescript.html.erb +1 -0
  26. data/spec/dummy/app/views/script_assets/custom_script.html.erb +1 -0
  27. data/spec/dummy/app/views/script_assets/javascript.html.erb +1 -0
  28. data/spec/dummy/app/views/script_assets/no_script.html.erb +2 -0
  29. data/spec/dummy/app/views/script_assets/production_mode.html.erb +1 -0
  30. data/spec/dummy/app/views/style_assets/css.html.erb +1 -0
  31. data/spec/dummy/app/views/style_assets/css_sass.html.erb +1 -0
  32. data/spec/dummy/app/views/style_assets/css_scss.html.erb +1 -0
  33. data/spec/dummy/app/views/style_assets/custom_style.html.erb +1 -0
  34. data/spec/dummy/app/views/style_assets/no_style.html.erb +2 -0
  35. data/spec/dummy/app/views/style_assets/production_mode.html.erb +1 -0
  36. data/spec/dummy/app/views/style_assets/sass.html.erb +1 -0
  37. data/spec/dummy/app/views/style_assets/scss.html.erb +1 -0
  38. data/spec/dummy/app/views/view_types/erb.html.erb +1 -0
  39. data/spec/dummy/app/views/view_types/haml.html.haml +1 -0
  40. data/spec/dummy/config.ru +4 -0
  41. data/spec/dummy/config/application.rb +18 -0
  42. data/spec/dummy/config/boot.rb +7 -0
  43. data/spec/dummy/config/environment.rb +5 -0
  44. data/spec/dummy/config/environments/development.rb +27 -0
  45. data/spec/dummy/config/environments/test.rb +37 -0
  46. data/spec/dummy/config/initializers/sections_rails.rb +0 -0
  47. data/spec/dummy/config/routes.rb +4 -0
  48. data/spec/dummy/log/development.log +16367 -0
  49. data/spec/dummy/tmp/cache/assets/CB9/B60/sprockets%2Fe7b839a0806e5c20e5018197f56cd656 +0 -0
  50. data/spec/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  51. data/spec/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  52. data/spec/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  53. data/spec/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  54. data/spec/dummy/tmp/cache/assets/D76/4D0/sprockets%2F8a096b6dd59bfda3e461617a95524eaf +0 -0
  55. data/spec/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  56. data/spec/dummy/tmp/cache/assets/DE1/6A0/sprockets%2Fcae9aba95894da8a28fa8a5387dc565f +0 -0
  57. data/spec/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  58. data/spec/dummy/tmp/cache/assets/E1D/010/sprockets%2Fdffad412d86bfdfbb1f307d711da21d3 +0 -0
  59. data/spec/sections_rails/section_spec.rb +24 -1
  60. data/spec/spec_helper.rb +3 -1
  61. metadata +137 -14
@@ -23,6 +23,12 @@ module SectionsRails
23
23
  @filename ||= File.basename @section_name, '.*'
24
24
  end
25
25
 
26
+ # Path to the folder for asset includes.
27
+ # Example: 'folder/section'
28
+ def folder_includepath
29
+ @folder_includepath ||= File.join directory_name, filename
30
+ end
31
+
26
32
  # Path of the folder on the file system.
27
33
  # Example: 'app/sections/folder/section'
28
34
  def folder_filepath
@@ -44,8 +50,14 @@ module SectionsRails
44
50
 
45
51
  # The path for accessing the partial on the filesystem.
46
52
  # Example: '
47
- def partial_filepath
48
- @partial_filepath ||= File.join(SectionsRails.config.path, directory_name,filename, "_#{filename}").gsub(/^\//, '')
53
+ def partial_renderpath partial_filename = nil
54
+ File.join(directory_name, filename, partial_filename || filename).gsub(/^\//, '')
55
+ end
56
+
57
+ # The path for accessing the partial on the filesystem.
58
+ # Example: '
59
+ def partial_filepath partial_filename = nil
60
+ File.join(SectionsRails.config.path, directory_name,filename, "_#{partial_filename or filename}").gsub(/^\//, '')
49
61
  end
50
62
 
51
63
  # For including the partial into views.
@@ -76,14 +88,24 @@ module SectionsRails
76
88
  end
77
89
 
78
90
  # Returns the filename of the partial of this section, or nil if this section has no partial.
79
- def find_partial_filepath
91
+ # Uses the given custom partial name, or the default partial name if none is given.
92
+ def find_partial_filepath partial_filename = nil
80
93
  SectionsRails.config.partial_extensions.each do |ext|
81
- path = "#{partial_filepath}.#{ext}"
94
+ path = "#{partial_filepath(filename)}.#{ext}"
82
95
  return path if File.exists? path
83
96
  end
84
97
  nil
85
98
  end
86
99
 
100
+ # Returns the path of the partial of this section for rendering, or nil if this section has no partial.
101
+ # Uses the given custom partial name, or the default partial name if none is given.
102
+ def find_partial_renderpath partial_filename = nil
103
+ SectionsRails.config.partial_extensions.each do |ext|
104
+ return partial_renderpath(partial_filename) if File.exists? "#{partial_filepath(filename)}.#{ext}"
105
+ end
106
+ nil
107
+ end
108
+
87
109
  # Returns a list of all section names in the given text.
88
110
  #
89
111
  # @param [ String ] text
@@ -123,54 +145,62 @@ module SectionsRails
123
145
  @view.lookup_context.template_exists? partial_includepath
124
146
  end
125
147
 
126
- # TODO(SZ): missing specs.
127
148
  def render
128
149
  result = []
129
150
 
151
+ # Check if section exists.
152
+ raise "Section #{folder_filepath} doesn't exist." unless Dir.exists? folder_filepath
153
+
130
154
  # Include assets only for development mode.
131
155
  if Rails.env != 'production'
132
156
 
133
157
  # Include JS assets.
134
- if @options[:js]
135
- result << @view.javascript_include_tag(File.join(path, js))
136
- elsif @options[:js] == false
137
- # ":js => false" given --> don't include any JS.
158
+ if @options.has_key? :js
159
+ if @options[:js]
160
+ result << @view.javascript_include_tag(File.join(folder_includepath, @options[:js]))
161
+ else
162
+ # :js => (false|nil) given --> don't include any JS.
163
+ end
138
164
  else
165
+ # No :js configuration option given --> include the default script.
139
166
  js_includepath = find_js_includepath
140
167
  result << @view.javascript_include_tag(js_includepath) if js_includepath
141
168
  end
142
169
 
143
170
  # Include CSS assets.
144
- if @options[:css]
145
- result << @view.stylesheet_link_tag(File.join(path, css))
146
- elsif @options[:css] == false
147
- # ":css => false" given --> don't include any CSS.
171
+ if @options.has_key? :css
172
+ if @options[:css]
173
+ # Custom filename for :css given --> include the given CSS file.
174
+ result << @view.stylesheet_link_tag(File.join(folder_includepath, @options[:css]))
175
+ else
176
+ # ":css => false" given --> don't include any CSS.
177
+ end
148
178
  else
179
+ # No option for :css given --> include the default stylesheet.
149
180
  css_includepath = find_css_includepath
150
181
  result << @view.stylesheet_link_tag(css_includepath) if css_includepath
151
182
  end
152
183
  end
153
184
 
154
185
  # Render the section partial into the view.
155
- case @options[:partial]
156
- when :tag
186
+ if @options.has_key? :partial
187
+ if @options[:partial] == :tag
188
+ # :partial => :tag given --> render the empty tag even if there is a partial present.
157
189
  result << @view.content_tag(:div, '', :class => filename)
158
-
159
- when false
160
- # partial: false given --> render nothing
161
-
162
- when nil
163
- # partial: nil given --> render default partial
164
- partial_filepath = find_partial_filepath
165
- if partial_filepath
166
- result << @view.render(:partial => partial_includepath, :locals => @options[:locals])
167
- else
168
- result << @view.content_tag(:div, '', :class => filename)
169
- end
170
-
190
+ elsif @options[:partial]
191
+ # Custom partial name given --> render that partial.
192
+ result << @view.render(find_partial_renderpath(@options[:partial]), @options[:locals])
193
+ else
194
+ # :partial => (false|nil) given --> render nothing.
195
+ end
196
+ else
197
+ # No :partial option given --> render the default partial.
198
+ partial_filepath = find_partial_filepath
199
+ if partial_filepath
200
+ result << @view.render(:partial => partial_includepath, :locals => @options[:locals])
171
201
  else
172
- # partial: custom path given --> render custom partial
173
- result << @view.render("#{path}/#{partial}", @options[:locals])
202
+ result << @view.content_tag(:div, '', :class => filename)
203
+ end
174
204
  end
175
205
 
176
206
  result.join("\n").html_safe
@@ -1,3 +1,3 @@
1
1
  module SectionsRails
2
- VERSION = "0.6.10"
2
+ VERSION = "0.6.11"
3
3
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe ErrorsController do
4
+ render_views
5
+
6
+ context "referenced section doesn't exist" do
7
+ it 'throws an exception' do
8
+ expect { get :missing_section; puts response.body }.to raise_error
9
+ end
10
+ end
11
+
12
+ end
13
+
14
+
15
+
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe PartialsController do
4
+ render_views
5
+
6
+ describe 'supported partial types' do
7
+ after :each do
8
+ response.should be_success
9
+ end
10
+
11
+ it 'allows to use ERB partials' do
12
+ get :erb_section
13
+ response.body.strip.should == 'ERB partial content'
14
+ end
15
+
16
+ it 'allows to use HAML partials' do
17
+ get :haml_section
18
+ response.body.strip.should == 'HAML partial content'
19
+ end
20
+ end
21
+
22
+ context 'no partial options given' do
23
+ it 'renders the default partial' do
24
+ get :no_options
25
+ response.body.strip.should == 'default partial content'
26
+ end
27
+ end
28
+
29
+ context 'providing a custom partial name' do
30
+ before :each do
31
+ get :custom_partial
32
+ end
33
+
34
+ it 'renders the given partial' do
35
+ response.body.should include 'custom partial content'
36
+ end
37
+
38
+ it "doesn't render the default partial" do
39
+ response.body.should_not include 'default partial content'
40
+ end
41
+ end
42
+
43
+ context 'disabling partials for a section' do
44
+ before :each do
45
+ get :disabled
46
+ end
47
+
48
+ it "doesn't render the partial tag" do
49
+ response.body.strip.should_not include '<div class="disabled">'
50
+ end
51
+
52
+ it "doesn't render the partial" do
53
+ response.body.strip.should_not include 'disabled partial content'
54
+ end
55
+ end
56
+
57
+ context 'production mode' do
58
+
59
+ before :each do
60
+ Rails.env = 'production'
61
+ end
62
+
63
+ after :each do
64
+ Rails.env = 'test'
65
+ end
66
+
67
+ it 'renders partials normally' do
68
+ get :production_mode
69
+ response.body.strip.should include 'partial content'
70
+ end
71
+ end
72
+ end
73
+
74
+
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe ScriptAssetsController do
4
+ render_views
5
+
6
+ before :each do
7
+ RSpec::Matchers.define :have_script_tag do |tag_name|
8
+ match do |body|
9
+ body =~ Regexp.new("<script.*src=\"#{tag_name}\".*<\/script>")
10
+ end
11
+ end
12
+ end
13
+
14
+ after :each do
15
+ response.should be_success
16
+ end
17
+
18
+ describe 'supported script languages' do
19
+ it 'allows to use JavaScript assets' do
20
+ get :javascript
21
+ response.body.should have_script_tag '/assets/script_assets/javascript/javascript.js'
22
+ end
23
+
24
+ it 'allows to use CoffeeScript assets' do
25
+ get :coffeescript
26
+ response.body.should have_script_tag '/assets/script_assets/coffeescript/coffeescript.js'
27
+ end
28
+ end
29
+
30
+ context 'setting a custom script filename' do
31
+ before :each do
32
+ get :custom_script
33
+ end
34
+
35
+ it 'includes the given script file into the page' do
36
+ response.body.should have_script_tag '/assets/script_assets/custom_script/different_name.js'
37
+ end
38
+
39
+ it "doesn't include the default script file into the page" do
40
+ response.body.should_not have_script_tag '/assets/script_assets/custom_script/custom_script.js'
41
+ end
42
+ end
43
+
44
+ context 'setting the script configuration option to false' do
45
+ it "doesn't include any script tag into the page" do
46
+ get :no_script
47
+ response.body.should_not match /<script/
48
+ end
49
+ end
50
+
51
+ context 'production mode' do
52
+ before :each do
53
+ Rails.env = 'production'
54
+ end
55
+
56
+ after :each do
57
+ Rails.env = 'test'
58
+ end
59
+
60
+ it "doesn't render script assets" do
61
+ get :production_mode
62
+ response.should_not have_script_tag '/assets/script_assets/production_mode/production_mode.js'
63
+ end
64
+ end
65
+ end
66
+
67
+
68
+
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe StyleAssetsController do
4
+ render_views
5
+
6
+ before :each do
7
+ RSpec::Matchers.define :have_style_tag do |tag_name|
8
+ match do |body|
9
+ body =~ Regexp.new("<link.*href=\"#{tag_name}\".*>")
10
+ end
11
+ end
12
+ end
13
+
14
+ after :each do
15
+ response.should be_success
16
+ end
17
+
18
+ describe 'supported style languages' do
19
+ it 'CSS assets' do
20
+ get :css
21
+ response.body.should have_style_tag '/assets/style_assets/css/css.css'
22
+ end
23
+
24
+ it 'SASS assets' do
25
+ get :sass
26
+ response.body.should have_style_tag '/assets/style_assets/sass/sass.css'
27
+ end
28
+
29
+ it 'CSS.SASS assets' do
30
+ get :css_sass
31
+ response.body.should have_style_tag '/assets/style_assets/css_sass/css_sass.css'
32
+ end
33
+
34
+ it 'SCSS assets' do
35
+ get :scss
36
+ response.body.should have_style_tag '/assets/style_assets/scss/scss.css'
37
+ end
38
+
39
+ it 'CSS.SCSS assets' do
40
+ get :css_scss
41
+ response.body.should have_style_tag '/assets/style_assets/css_scss/css_scss.css'
42
+ end
43
+ end
44
+
45
+ context 'setting a custom style filename' do
46
+ before :each do
47
+ get :custom_style
48
+ end
49
+
50
+ it 'includes the given style file into the page' do
51
+ response.body.should have_style_tag '/assets/style_assets/custom_style/different_name.css'
52
+ end
53
+
54
+ it "doesn't include the default script file into the page" do
55
+ response.body.should_not have_style_tag '/assets/style_assets/custom_style/custom_style.css'
56
+ end
57
+ end
58
+
59
+ context 'setting the script configuration option to false' do
60
+ it "doesn't include any script tag into the page" do
61
+ get :no_style
62
+ response.body.should_not have_style_tag '/assets/style_assets/no_style/no_style.css'
63
+ end
64
+ end
65
+
66
+ context 'production_mode' do
67
+
68
+ before :each do
69
+ Rails.env = "production"
70
+ end
71
+
72
+ after :each do
73
+ Rails.env = "test"
74
+ end
75
+
76
+ it "doesn't render style assets" do
77
+ get :production_mode
78
+ response.body.should_not have_style_tag '/assets/style_assets/production_mode/production_mode.css'
79
+ end
80
+ end
81
+ end
82
+
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe ViewTypesController do
4
+ render_views
5
+
6
+ describe 'supported view types' do
7
+ after :each do
8
+ response.should be_success
9
+ end
10
+
11
+ it 'allows to use the section helper in ERB views' do
12
+ get :erb
13
+ response.body.strip.should == 'Foo partial content'
14
+ end
15
+
16
+ it 'allows to use the section helper in HAML views' do
17
+ get :haml
18
+ response.body.strip.should == 'Foo partial content'
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */