sections_rails 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/README.md +82 -92
  2. data/lib/sections_rails/section.rb +11 -174
  3. data/lib/sections_rails/section_asset_rendering.rb +92 -0
  4. data/lib/sections_rails/section_partial_rendering.rb +100 -0
  5. data/lib/sections_rails/version.rb +1 -1
  6. data/lib/sections_rails/view_finder.rb +10 -8
  7. data/lib/sections_rails.rb +1 -1
  8. data/spec/controllers/image_assets_controller_spec.rb +38 -0
  9. data/spec/controllers/partials_controller_spec.rb +25 -11
  10. data/spec/controllers/script_assets_controller_spec.rb +7 -7
  11. data/spec/controllers/style_assets_controller_spec.rb +10 -10
  12. data/spec/controllers/view_types_controller_spec.rb +3 -3
  13. data/spec/dummy/app/controllers/image_assets_controller.rb +14 -0
  14. data/spec/dummy/app/controllers/partials_controller.rb +6 -0
  15. data/spec/dummy/app/views/image_assets/gif.html.erb +1 -0
  16. data/spec/dummy/app/views/image_assets/jpeg.html.erb +1 -0
  17. data/spec/dummy/app/views/image_assets/jpg.html.erb +1 -0
  18. data/spec/dummy/app/views/image_assets/png.html.erb +1 -0
  19. data/spec/dummy/app/views/partials/custom_partial_with_block.html.erb +3 -0
  20. data/spec/dummy/app/views/partials/tag_option.html.erb +1 -0
  21. data/spec/dummy/config/application.rb +4 -3
  22. data/spec/dummy/config/boot.rb +7 -3
  23. data/spec/dummy/config/environments/test.rb +1 -4
  24. data/spec/dummy/db/development.sqlite3 +0 -0
  25. data/spec/dummy/db/test.sqlite3 +0 -0
  26. data/spec/dummy/log/development.log +3204 -16064
  27. data/spec/dummy/log/production.log +61 -0
  28. data/spec/dummy/log/test.log +2041 -0
  29. data/spec/sections_rails/config_spec.rb +31 -0
  30. data/spec/sections_rails/partial_parser_spec.rb +12 -12
  31. data/spec/sections_rails/section_spec.rb +16 -54
  32. data/spec/sections_rails/view_finder_spec.rb +4 -4
  33. data/spec/spec_helper.rb +3 -0
  34. metadata +73 -35
  35. data/spec/dummy/tmp/cache/assets/CB9/B60/sprockets%2Fe7b839a0806e5c20e5018197f56cd656 +0 -0
  36. data/spec/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  37. data/spec/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  38. data/spec/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  39. data/spec/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  40. data/spec/dummy/tmp/cache/assets/D76/4D0/sprockets%2F8a096b6dd59bfda3e461617a95524eaf +0 -0
  41. data/spec/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  42. data/spec/dummy/tmp/cache/assets/DE1/6A0/sprockets%2Fcae9aba95894da8a28fa8a5387dc565f +0 -0
  43. data/spec/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  44. data/spec/dummy/tmp/cache/assets/E1D/010/sprockets%2Fdffad412d86bfdfbb1f307d711da21d3 +0 -0
data/README.md CHANGED
@@ -1,38 +1,33 @@
1
- _SectionsRails_ adds a component-oriented infrastructure to the view layer of Ruby on Rails.
1
+ # Sections for Rails [![Build Status](https://travis-ci.org/kevgo/sections_rails.png?branch=master)](https://travis-ci.org/kevgo/sections_rails) [![Code Climate](https://codeclimate.com/github/kevgo/sections_rails.png)](https://codeclimate.com/github/kevgo/sections_rails) [![Coverage Status](https://coveralls.io/repos/kevgo/sections_rails/badge.png?branch=master)](https://coveralls.io/r/kevgo/sections_rails)
2
2
 
3
- In short, the DOM, styling, behavior, tests, and other data for dedicated pieces of a web page are defined together, in one directory,
4
- rather than spread across _app/views_, _app/assets/javascripts_, _app/assets/stylesheets_, and _spec/javascripts_.
5
- This makes it easier to work on those pieces, makes them more reusable, and large code bases more managable.
3
+ _A component-oriented infrastructure for the view layer of Ruby on Rails applications._
6
4
 
5
+ A _section_ organizes all assets (DOM, styling, behavior, tests etc) for a particular component of a web site (a menu, a navbar, a slideshow etc)
6
+ together. This makes development easier, components more reusable, and large code bases more managable.
7
7
 
8
- # Example
9
8
 
10
- Let's take the navigation menu within a web site as an example section.
11
- It consists of certain HTML, CSS, and JavaScript code as well as image resources.
12
- These assets must be loaded on every page that this navigation menu is visible on,
13
- and should be removed when the navigation menu is removed from the site.
9
+ ## Example
14
10
 
15
- Traditionally, these files are defined in this directory structure:
11
+ A navigation menu (as an example for a component) within a web site consists of HTML, CSS, and JavaScript code, image resources, as well as unit tests.
12
+ Traditionally, these files are defined in a directory like this:
16
13
 
17
- ```
18
- /app/assets/javascripts/menu.js
19
- /templates/entry.jst.ejs
20
- /stylesheets/menu.css
21
- /views/shared/_menu.html.erb
22
- /doc/menu.md
23
- /spec/javascripts/menu_spec.js
24
- ```
14
+ /app/assets/images/background.gif # Background image.
15
+ /javascripts/menu.js # Logic for the menu.
16
+ /templates/entry.jst.ejs # Client-side template for menu entries.
17
+ /stylesheets/menu.css # Styling.
18
+ /views/shared/_menu.html.erb # Server-side template.
19
+ /spec/javascripts/menu_spec.js # Unit tests.
25
20
 
26
- _Sections_rails_ allows to define these assets together, as a _section_ inside the _/app_ folder:
21
+ _Sections_rails_ allows to define these assets together, as a _section_, inside the _/app_ folder:
27
22
 
28
- /app/sections/menu/_menu.html.erb # Server side template.
29
- menu.css # Styling for the menu.
30
- menu.js # Logic for the menu.
31
- readme.md # Documentation.
32
- entry.jst.ejs # Client-side template.
33
- menu_spec.coffee # Unit test for logic of this template.
23
+ /app/sections/menu/_menu.html.erb
24
+ background.gif
25
+ entry.jst.ejs
26
+ menu.css
27
+ menu.js
28
+ menu_spec.coffee
34
29
 
35
- To embed this menu and all its assets into a page, simply do this in your view:
30
+ To embed this menu into a page, simply do this in your view:
36
31
 
37
32
  ```erb
38
33
  <%= section :menu %>
@@ -40,144 +35,139 @@ To embed this menu and all its assets into a page, simply do this in your view:
40
35
 
41
36
  This command inserts the partial as well as the JS and CSS files from _/app/sections/menu_ into the page.
42
37
 
43
- It does the right thing in all circumstances: In development mode it inserts the individual assets,
44
- in production mode the assets are included into the precompilation targets.
38
+ It does the right thing in all circumstances: In development mode it inserts the individual files into the page,
39
+ in production mode they are included into the compiled output.
40
+ And when the menu folder is deleted or moved to another page,
41
+ all assets of the menu are cleanly deleted or moved with it!
45
42
 
46
- The gem source comes with a bundled example Rails app in the _demo/_ directory.
47
- It provides several working examples of sections in action in _views/demos/index.html.erb_.
48
43
 
44
+ ## Example applications
49
45
 
50
- # Installation
46
+ The gem source comes with a bundled example Rails app in the [demo](https://github.com/kevgo/sections_rails/tree/master/demo) directory.
47
+ Start it up and [check it out](http://localhost:3000)!
48
+ The source code for the sections is [here](https://github.com/kevgo/sections_rails/tree/master/demo/app/sections).
51
49
 
52
- In your Gemfile:
53
50
 
54
- ```ruby
55
- gem 'sections_rails'
56
- ```
51
+ ## Installation
57
52
 
58
- Then set up the directory structure:
53
+ In your Gemfile: `gem 'sections_rails'`, then set up the directory structure:
59
54
 
60
55
  ```bash
61
56
  $ rails generate sections
62
57
  ```
63
58
 
64
- The generator does the following things:
59
+ The generator
65
60
 
66
- 1. It creates a new folder __/app/sections__,
67
- in which you put the source code for the different sections.
61
+ * creates a new folder __/app/sections__, in which you put the source code for the different sections.
68
62
 
69
- 2. It adds the folder _/app/sections_ to the asset pipeline by inserting this line into your _application.rb_ file:
63
+ * adds the folder _/app/sections_ to the asset pipeline by inserting this line into your _application.rb_ file:
70
64
 
71
65
  config.assets.paths << 'app/sections'
72
66
 
73
- 3. It optionally creates a demo section called _hello_world_ that you can try out as described below.
67
+ * optionally creates a demo section called _hello_world_ that you can try out as described below.
74
68
 
75
69
 
76
- In it's current prototypical implementation, _Sections_rails_ also creates empty asset container files:
77
- __application_sections.js__ and __application_sections.css__.
70
+ The current implementation requires two empty asset container files: __application_sections.js__ and __application_sections.css__.
78
71
  Make sure you require them from your main _application.js_ and _application.css_ files.
79
- They are used only when running _rake assets:precompile_ during deployment, and should be checked in and stay the way they are.
72
+ They are used only when running _rake assets:precompile_ during deployment, should be checked into your code repository, and stay the way they are.
80
73
 
81
74
 
82
- # Usage
75
+ ## Usage
83
76
 
84
- To use the "hello_world" section created by the sections generator, simply add it to the view:
77
+ To show the "hello_world" section created by the sections generator on a page:
85
78
 
86
79
  ```erb
87
80
  <%= section :hello_world %>
88
81
  ```
89
82
 
90
83
  If your section renders itself completely in JavaScript, you can omit its partial file.
91
- In this case, the _sections_ helper creates an empty div in the view.
84
+ In this case, the _sections_ helper creates an empty div with the section name as its class.
92
85
 
93
86
  ```html
94
87
  <div class="hello_world"></div>
95
88
  ```
96
89
 
97
- ## Options
98
-
99
90
  By default, a section automatically includes partials, css, and js files with the section name if they exist.
100
- This convention can be overridden. The following example renders the _hello_world_ section with a different partial, with no stylesheet,
101
- and it uses the custom _foobar.js_ instead of _hello_world.js_.
91
+ This convention can be overridden.
92
+
93
+
94
+ ### Customizing the filename of the section's partial.
102
95
 
103
96
  ```erb
104
- <%= section :hello_world, partial: 'hello_new', css: false, js: 'foobar.js' %>
97
+ <%= section :hello_world, partial: 'another_partial' %>
105
98
  ```
106
99
 
107
- It is also possible to provide parameters to the rendered partial.
100
+
101
+ ### Customizing the filename of the assets
102
+
103
+ To tell the section to use `foobar.js` instead of `hello_world.js`:
108
104
 
109
105
  ```erb
110
- <%= section :hello_world, locals: { message: 'Greetings!' } %>
106
+ <%= section :hello_world, js: 'foobar.js' %>
111
107
  ```
112
108
 
113
- You can also provide a block to the section, which can be included into the partial by saying
114
109
 
110
+ ### Omitting assets
115
111
 
116
- ## Inline blocks for sections.
112
+ Sections can be told not not include their css or js file when rendered.
117
113
 
118
- You can provide a block to the section, like so:
114
+ ```erb
115
+ <%= section :hello_world, css: false %>
116
+ ```
117
+
118
+ ### Providing parameters to the partial
119
119
 
120
120
  ```erb
121
- <%= section :section_with_block do %>
122
- <h1> Elaborate </h1>
123
- <div> Block </div>
124
- <p> content </p>
125
- <% end %>
121
+ <%= section :hello_world, locals: { message: 'Greetings!' } %>
126
122
  ```
127
123
 
128
- This block can be included into the partial:
124
+ ### Inline blocks for sections.
125
+
126
+ You can provide a block to the section:
127
+
129
128
  ```erb
130
- Partial content...
131
- <%= yield %>
132
- Partial content...
129
+ <%= section :info_window do %>
130
+ <h1> inline </h1>
131
+ <div> block </div>
132
+ <p> content </p>
133
+ <% end %>
133
134
  ```
134
135
 
135
- This allows to define the content of parts of the section dynamically in the calling view, using view helper methods.
136
+ This block can be included into the serverside partial of the section using `yield`.
136
137
 
137
138
 
138
- ## Creating new sections.
139
+ ### Creating new sections.
139
140
 
140
- To create a new section, simply create a new folder under _/app/sections_ and add the partials, css, js, jst, and test files for this section.
141
- Alternatively, run the provided generator:
141
+ To create a new section, simply create a new folder under _/app/sections_.
142
+ There is also a generator for your convenience, which creates a whole scaffolded section.
142
143
 
143
144
  ```bash
144
145
  $ rails generate section admin/chart
145
146
  ```
146
147
 
147
- This creates a folder _/app/sections/admin/chart_ with a scaffold for a new section.
148
-
149
148
 
150
- # Unit tests for sections
149
+ ### Unit tests for sections
151
150
 
152
151
  Sections should also contain unit test files for the section.
153
152
 
154
- ## Unit testing using Konacha
155
-
156
- _This feature is still under development._
157
-
158
- To test them for example using [Konacha](https://github.com/jfirebaugh/konacha), create a symlink to _app/sections_ in _spec/javascript_.
159
-
153
+ To run your section tests using [Konacha](https://github.com/jfirebaugh/konacha), create a symlink to _app/sections_ in _spec/javascript_.
160
154
 
161
- # Development
162
155
 
163
- ## Unit tests
156
+ ## How to contribute
164
157
 
165
- ```bash
166
- $ rake
167
- ```
158
+ Feel free to send unit-tested pull requests! The unit tests of this project are run using `rake`.
168
159
 
169
- To automatically run unit tests when files change, run
170
160
 
171
- ```bash
172
- $ bundle exec guard -c
173
- ```
161
+ ### Missing features
174
162
 
175
- # Missing features
163
+ _Sections_rails_ is far from complete. Some missing things are:
176
164
 
177
- _Sections_rails_ is in prototypical development and far from complete. Missing features are:
178
-
179
- * Better support for unit testing.
180
165
  * Support for multiple application assets, for example page-specific compiled asset files instead of one global one.
181
166
  * Support for serverside controller logic for sections, for example by integrating with https://github.com/apotonick/cells.
182
167
  * More natural integration into the asset pipeline.
183
168
 
169
+
170
+ ### Authors
171
+
172
+ * [Kevin Goslar](https://github.com/kevgo)
173
+ * [Serge Zinin](https://github.com/zininserge)
@@ -1,8 +1,14 @@
1
1
  module SectionsRails
2
2
  require "sections_rails/config"
3
3
  require 'sections_rails/partial_parser'
4
+ require 'sections_rails/section_asset_rendering'
5
+ require 'sections_rails/section_partial_rendering'
4
6
 
7
+
8
+ # Provides intelligent support for dealing with sections.
5
9
  class Section
10
+ include SectionAssetRenderering
11
+ include SectionPartialRendering
6
12
 
7
13
  def initialize section_name, view = nil, options = {}
8
14
  @section_name = section_name.to_s
@@ -24,122 +30,12 @@ module SectionsRails
24
30
  @filename ||= File.basename @section_name, '.*'
25
31
  end
26
32
 
27
- # Path to the folder for asset includes.
28
- # Example: 'folder/section'
29
- def folder_includepath
30
- @folder_includepath ||= File.join directory_name, filename
31
- end
32
-
33
33
  # Path of the folder on the file system.
34
34
  # Example: 'app/sections/folder/section'
35
35
  def folder_filepath
36
36
  @folder_filepath ||= File.join SectionsRails.config.path, directory_name, filename
37
37
  end
38
38
 
39
- # Path to access assets on the file system.
40
- # Includes only the base name of assets, without extensions.
41
- # Example: 'app/sections/folder/section/section'
42
- def asset_filepath
43
- @asset_filepath ||= File.join SectionsRails.config.path, asset_includepath
44
- end
45
-
46
- # Path for including assets into the web page.
47
- #
48
- def asset_includepath
49
- @asset_includepath ||= File.join(directory_name, filename, filename).gsub(/^\//, '')
50
- end
51
-
52
- # The path for accessing the partial on the filesystem.
53
- # Example: '
54
- def partial_renderpath partial_filename = nil
55
- File.join(directory_name, filename, partial_filename || filename).gsub(/^\//, '')
56
- end
57
-
58
- # The path for accessing the partial on the filesystem.
59
- # Example: '
60
- def partial_filepath partial_filename = nil
61
- File.join(SectionsRails.config.path, directory_name,filename, "_#{partial_filename or filename}").gsub(/^\//, '')
62
- end
63
-
64
- # For including the partial into views.
65
- def partial_includepath
66
- @partial_includepath ||= File.join(directory_name, filename, "#{filename}").gsub(/^\//, '')
67
- end
68
-
69
- # Returns the content of this sections partial.
70
- def partial_content
71
- return @partial_content if @has_partial_content
72
- @has_partial_content = true
73
- if (partial_path = find_partial_filepath)
74
- @partial_content = IO.read partial_path
75
- end
76
- end
77
-
78
- # Returns the asset path of asset with the given extensions.
79
- # Helper method.
80
- def find_asset_includepath asset_option, extensions
81
- return nil if asset_option == false
82
- return asset_option if asset_option
83
- extensions.each do |ext|
84
- file_path = "#{asset_filepath}.#{ext}"
85
- return asset_includepath if File.exists? file_path
86
- end
87
- nil
88
- end
89
-
90
- # Returns the path to the JS asset of this section, or nil if the section doesn't have one.
91
- def find_css_includepath
92
- @find_css_includepath ||= find_asset_includepath @options[:css], SectionsRails.config.css_extensions
93
- end
94
-
95
- # Returns the path to the JS asset of this section, or nil if the section doesn't have one.
96
- def find_js_includepath
97
- @find_js_includepath ||= find_asset_includepath @options[:js], SectionsRails.config.js_extensions
98
- end
99
-
100
- # Returns the filename of the partial of this section, 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_filepath partial_filename = nil
103
- SectionsRails.config.partial_extensions.each do |ext|
104
- path = "#{partial_filepath(filename)}.#{ext}"
105
- return path if File.exists? path
106
- end
107
- nil
108
- end
109
-
110
- # Returns the path of the partial of this section for rendering, or nil if this section has no partial.
111
- # Uses the given custom partial name, or the default partial name if none is given.
112
- def find_partial_renderpath partial_filename = nil
113
- SectionsRails.config.partial_extensions.each do |ext|
114
- return partial_renderpath(partial_filename) if File.exists? "#{partial_filepath(filename)}.#{ext}"
115
- end
116
- nil
117
- end
118
-
119
- # TODO: replace this with find_asset.
120
- def has_asset? *extensions
121
- extensions.flatten.each do |ext|
122
- return true if File.exists?("#{asset_filepath}.#{ext}")
123
- end
124
- false
125
- end
126
-
127
- # TODO: replace this with find_asset.
128
- def has_default_js_asset?
129
- has_asset? SectionsRails.config.js_extensions
130
- end
131
-
132
- # TODO: replace this with find_asset.
133
- def has_default_style_asset?
134
- has_asset? SectionsRails.config.css_extensions
135
- end
136
-
137
- # Returns whether this section has a template.
138
- # Deprecated.
139
- def has_partial?
140
- @view.lookup_context.template_exists? partial_includepath
141
- end
142
-
143
39
  # Returns the sections that this section references.
144
40
  # If 'recursive = true' is given, searches recursively for sections referenced by the referenced sections.
145
41
  # Otherwise, simply returns the sections that are referenced by this section.
@@ -158,73 +54,14 @@ module SectionsRails
158
54
  result.sort!
159
55
  end
160
56
 
161
- def render &block
162
- result = []
163
57
 
164
- # Check if section exists.
58
+ # Renders this section, i.e. returns the HTML for this section.
59
+ def render &block
165
60
  raise "Section #{folder_filepath} doesn't exist." unless Dir.exists? folder_filepath
166
61
 
167
- # Include assets only for development mode.
168
- if Rails.env != 'production'
169
-
170
- # Include JS assets.
171
- if @options.has_key? :js
172
- if @options[:js]
173
- result << @view.javascript_include_tag(File.join(folder_includepath, @options[:js]))
174
- else
175
- # :js => (false|nil) given --> don't include any JS.
176
- end
177
- else
178
- # No :js configuration option given --> include the default script.
179
- js_includepath = find_js_includepath
180
- result << @view.javascript_include_tag(js_includepath) if js_includepath
181
- end
182
-
183
- # Include CSS assets.
184
- if @options.has_key? :css
185
- if @options[:css]
186
- # Custom filename for :css given --> include the given CSS file.
187
- result << @view.stylesheet_link_tag(File.join(folder_includepath, @options[:css]))
188
- else
189
- # ":css => false" given --> don't include any CSS.
190
- end
191
- else
192
- # No option for :css given --> include the default stylesheet.
193
- css_includepath = find_css_includepath
194
- result << @view.stylesheet_link_tag(css_includepath) if css_includepath
195
- end
196
- end
197
-
198
- # Render the section partial into the view.
199
- if @options.has_key? :partial
200
- if @options[:partial] == :tag
201
- # :partial => :tag given --> render the empty tag even if there is a partial present.
202
- result << @view.content_tag(:div, '', :class => filename)
203
- elsif @options[:partial]
204
- # Custom partial name given --> render that partial.
205
- if block_given?
206
- result << @view.render({:layout => find_partial_renderpath(@options[:partial])}, @options[:locals], &block)
207
- else
208
- result << @view.render(find_partial_renderpath(@options[:partial]), @options[:locals])
209
- end
210
- else
211
- # :partial => (false|nil) given --> render nothing.
212
- end
213
- else
214
- # No :partial option given --> render the default partial.
215
- partial_filepath = find_partial_filepath
216
- if partial_filepath
217
-
218
- if block_given?
219
- result << @view.render(:layout => partial_includepath, :locals => @options[:locals], &block)
220
- else
221
- result << @view.render(:partial => partial_includepath, :locals => @options[:locals])
222
- end
223
- else
224
- result << @view.content_tag(:div, '', :class => filename)
225
- end
226
- end
227
-
62
+ result = []
63
+ render_assets result if Rails.env != 'production'
64
+ render_partial result, &block
228
65
  result.join("\n").html_safe
229
66
  end
230
67
  end
@@ -0,0 +1,92 @@
1
+ module SectionsRails
2
+
3
+ # Helps with rendering the assets of a section.
4
+ module SectionAssetRenderering
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+
9
+ # Path to access assets on the file system.
10
+ # Includes only the base name of assets, without extensions.
11
+ # Example: 'app/sections/folder/section/section'
12
+ def asset_filepath
13
+ @asset_filepath ||= File.join SectionsRails.config.path, asset_includepath
14
+ end
15
+
16
+ # Path for including assets into the web page.
17
+ #
18
+ def asset_includepath
19
+ @asset_includepath ||= File.join(directory_name, filename, filename).gsub(/^\//, '')
20
+ end
21
+
22
+
23
+ # Returns the path to the JS asset of this section, or nil if the section doesn't have one.
24
+ def find_js_includepath
25
+ @find_js_includepath ||= find_asset_includepath @options[:js], SectionsRails.config.js_extensions
26
+ end
27
+
28
+
29
+ # Returns the asset path of asset with the given extensions.
30
+ # Helper method.
31
+ def find_asset_includepath asset_option, extensions
32
+ return nil if asset_option == false
33
+ return asset_option if asset_option
34
+ extensions.each do |ext|
35
+ file_path = "#{asset_filepath}.#{ext}"
36
+ return asset_includepath if File.exists? file_path
37
+ end
38
+ nil
39
+ end
40
+
41
+ # Returns the path to the JS asset of this section, or nil if the section doesn't have one.
42
+ def find_css_includepath
43
+ @find_css_includepath ||= find_asset_includepath @options[:css], SectionsRails.config.css_extensions
44
+ end
45
+
46
+
47
+ # Path to the folder for asset includes.
48
+ # Example: 'folder/section'
49
+ def folder_includepath
50
+ @folder_includepath ||= File.join directory_name, filename
51
+ end
52
+
53
+
54
+ # Renders the assets of this section into the given result.
55
+ def render_assets result
56
+ render_js_assets result
57
+ render_css_assets result
58
+ end
59
+
60
+ # Renders the CSS for this section.
61
+ def render_css_assets result
62
+ if @options.has_key? :css
63
+ if @options[:css]
64
+ # Custom filename for :css given --> include the given CSS file.
65
+ result << @view.stylesheet_link_tag(File.join(folder_includepath, @options[:css]))
66
+ else
67
+ # ":css => false" given --> don't include any CSS.
68
+ end
69
+ else
70
+ # No option for :css given --> include the default stylesheet.
71
+ css_includepath = find_css_includepath
72
+ result << @view.stylesheet_link_tag(css_includepath) if css_includepath
73
+ end
74
+ end
75
+
76
+ # Renders the JS for this section.
77
+ def render_js_assets result
78
+ if @options.has_key? :js
79
+ if @options[:js]
80
+ result << @view.javascript_include_tag(File.join(folder_includepath, @options[:js]))
81
+ else
82
+ # :js => (false|nil) given --> don't include any JS.
83
+ end
84
+ else
85
+ # No :js configuration option given --> include the default script.
86
+ js_includepath = find_js_includepath
87
+ result << @view.javascript_include_tag(js_includepath) if js_includepath
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,100 @@
1
+ module SectionsRails
2
+
3
+ # Helps with rendering partials of sections.
4
+ module SectionPartialRendering
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+
9
+ # The path for accessing the partial on the filesystem.
10
+ # Example: '
11
+ def partial_renderpath partial_filename = nil
12
+ File.join(directory_name, filename, partial_filename || filename).gsub(/^\//, '')
13
+ end
14
+
15
+ # The path for accessing the partial on the filesystem.
16
+ # Example: '
17
+ def partial_filepath partial_filename = nil
18
+ File.join(SectionsRails.config.path, directory_name, filename, "_#{partial_filename or filename}").gsub(/^\//, '')
19
+ end
20
+
21
+ # For including the partial into views.
22
+ def partial_includepath
23
+ @partial_includepath ||= File.join(directory_name, filename, "#{filename}").gsub(/^\//, '')
24
+ end
25
+
26
+ # Returns the content of this sections partial.
27
+ def partial_content
28
+ return @partial_content if @has_partial_content
29
+ @has_partial_content = true
30
+ if (partial_path = find_partial_filepath)
31
+ @partial_content = IO.read partial_path
32
+ end
33
+ end
34
+
35
+ # Returns the filename of the partial of this section, or nil if this section has no partial.
36
+ # Uses the given custom partial name, or the default partial name if none is given.
37
+ def find_partial_filepath partial_filename = nil
38
+ SectionsRails.config.partial_extensions.each do |ext|
39
+ path = "#{partial_filepath(filename)}.#{ext}"
40
+ return path if File.exists? path
41
+ end
42
+ nil
43
+ end
44
+
45
+ # Returns the path of the partial of this section for rendering, or nil if this section has no partial.
46
+ # Uses the given custom partial name, or the default partial name if none is given.
47
+ def find_partial_renderpath partial_filename = nil
48
+ SectionsRails.config.partial_extensions.each do |ext|
49
+ return partial_renderpath(partial_filename) if File.exists? "#{partial_filepath(filename)}.#{ext}"
50
+ end
51
+ nil
52
+ end
53
+
54
+
55
+ # Renders the section partial into the view.
56
+ def render_partial result, &block
57
+ if @options.has_key? :partial
58
+ if @options[:partial] == :tag
59
+ # :partial => :tag given
60
+ render_empty_tag result
61
+ elsif @options[:partial]
62
+ # Custom partial name given
63
+ render_custom_partial result, &block
64
+ else
65
+ # :partial => (false|nil) given --> render nothing.
66
+ end
67
+ else
68
+ # No :partial option given
69
+ render_default_partial result, &block
70
+ end
71
+ end
72
+
73
+ def render_custom_partial result, &block
74
+ if block_given?
75
+ result << @view.render({:layout => find_partial_renderpath(@options[:partial])}, @options[:locals], &block)
76
+ else
77
+ result << @view.render(find_partial_renderpath(@options[:partial]), @options[:locals])
78
+ end
79
+ end
80
+
81
+ def render_default_partial result, &block
82
+ partial_filepath = find_partial_filepath
83
+ if partial_filepath
84
+
85
+ if block_given?
86
+ result << @view.render(:layout => partial_includepath, :locals => @options[:locals], &block)
87
+ else
88
+ result << @view.render(:partial => partial_includepath, :locals => @options[:locals])
89
+ end
90
+ else
91
+ result << @view.content_tag(:div, '', :class => filename)
92
+ end
93
+ end
94
+
95
+ def render_empty_tag result
96
+ result << @view.content_tag(:div, '', :class => filename)
97
+ end
98
+ end
99
+ end
100
+ end
@@ -1,3 +1,3 @@
1
1
  module SectionsRails
2
- VERSION = "0.7.1"
2
+ VERSION = "0.7.2"
3
3
  end