sections_rails 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +61 -12
- data/lib/sections_rails/version.rb +1 -1
- data/lib/sections_rails.rb +39 -7
- metadata +8 -8
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
_SectionsRails_ adds a component-oriented infrastructure to the view layer of Ruby on Rails.
|
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.
|
4
6
|
|
5
7
|
|
6
8
|
# Example
|
@@ -10,24 +12,39 @@ It consists of certain HTML, CSS, and JavaScript code as well as image resources
|
|
10
12
|
These assets must be loaded on every page that this navigation menu is visible on,
|
11
13
|
and should be removed when the navigation menu is removed from the site.
|
12
14
|
|
15
|
+
Traditionally, these files are defined in this directory structure:
|
16
|
+
|
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
|
+
```
|
25
|
+
|
13
26
|
_Sections_rails_ allows to define these assets together, as a _section_ inside the _/app_ folder:
|
14
27
|
|
15
|
-
/app/sections/menu/_menu.html.erb
|
16
|
-
menu.css
|
17
|
-
menu.js
|
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.
|
18
34
|
|
19
|
-
To
|
35
|
+
To embed this menu and all its assets into a page, simply do this in your view:
|
20
36
|
|
21
37
|
```erb
|
22
38
|
<%= section :menu %>
|
23
39
|
```
|
24
40
|
|
25
|
-
|
26
|
-
|
27
|
-
In
|
41
|
+
This command inserts the partial as well as the JS and CSS files from _/app/sections/menu_ into the page.
|
42
|
+
|
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.
|
28
45
|
|
29
46
|
The gem source comes with a bundled example Rails app in the _demo/_ directory.
|
30
|
-
It provides
|
47
|
+
It provides several working examples of sections in action in _views/demos/index.html.erb_.
|
31
48
|
|
32
49
|
|
33
50
|
# Installation
|
@@ -77,13 +94,45 @@ In this case, the _sections_ helper creates an empty div in the view.
|
|
77
94
|
<div class="hello_world"></div>
|
78
95
|
```
|
79
96
|
|
97
|
+
## Options
|
98
|
+
|
99
|
+
By default, a section automatically includes partials, css, and js files with the section name if they exist.
|
100
|
+
This convention can be overridden by the options __:partial__, __:css__, and __:js__ the `section` command.
|
101
|
+
Providing `false` disables the respective functionality, while a filename makes the section use the given file instead of the default one.
|
102
|
+
|
103
|
+
The command ```erb <%= section :hello_world, css: false, js: 'foobar.js' %>``` renders the _hello_world_ section without styling and using
|
104
|
+
_foobar.js_ instead of _hello_world.js_.
|
105
|
+
|
106
|
+
|
107
|
+
## Creating new sections.
|
108
|
+
|
109
|
+
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.
|
110
|
+
Alternatively, run the provided generator:
|
111
|
+
|
112
|
+
```bash
|
113
|
+
$ rails generate section admin/chart
|
114
|
+
```
|
115
|
+
|
116
|
+
This creates a folder _/app/sections/admin/chart_ with a scaffold for a new section.
|
117
|
+
|
118
|
+
|
119
|
+
# Unit tests for sections
|
120
|
+
|
121
|
+
Sections should also contain unit test files for the section.
|
122
|
+
|
123
|
+
## Unit testing using Konacha
|
124
|
+
|
125
|
+
_This feature is still under development._
|
126
|
+
|
127
|
+
To test them for example using [Konacha](https://github.com/jfirebaugh/konacha), create a symlink to _app/sections_ in _spec/javascript_.
|
128
|
+
|
80
129
|
|
81
130
|
# Missing features
|
82
131
|
|
83
132
|
_Sections_rails_ is in prototypical development and far from complete. Missing features are:
|
84
133
|
|
134
|
+
* Better support for unit testing.
|
85
135
|
* Support for multiple application assets, for example page-specific compiled asset files instead of one global one.
|
86
|
-
* Support for assets in different formats like CoffeeScript, Haml, Sass etc.
|
87
136
|
* Support for serverside controller logic for sections, for example by integrating with https://github.com/apotonick/cells.
|
88
137
|
* More natural integration into the asset pipeline.
|
89
138
|
|
data/lib/sections_rails.rb
CHANGED
@@ -2,7 +2,7 @@ module SectionsRails
|
|
2
2
|
|
3
3
|
require "sections_rails/railtie" if defined?(Rails)
|
4
4
|
|
5
|
-
def section combined_name
|
5
|
+
def section combined_name, options = {}
|
6
6
|
out = []
|
7
7
|
|
8
8
|
# Split the parameter into file name and directory name.
|
@@ -15,18 +15,50 @@ module SectionsRails
|
|
15
15
|
# Add assets of section when in dev mode.
|
16
16
|
file_path = "#{directory_path}/#{filename}" # Base path of filename in section: /app/sections/admin/logo/logo
|
17
17
|
if Rails.env != 'production'
|
18
|
-
|
19
|
-
|
18
|
+
|
19
|
+
# Include JS assets.
|
20
|
+
if options.has_key? :js
|
21
|
+
if options[:js]
|
22
|
+
out << javascript_include_tag("#{directory}#{filename}/#{options[:js]}")
|
23
|
+
end
|
24
|
+
else
|
25
|
+
if File.exists?("#{file_path}.js") || File.exists?("#{file_path}.js.coffee") || File.exists?("#{file_path}.coffee")
|
26
|
+
out << javascript_include_tag("#{directory}#{filename}/#{filename}")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Include CSS assets.
|
31
|
+
if options.has_key? :css
|
32
|
+
if options[:css]
|
33
|
+
out << stylesheet_link_tag("#{directory}#{filename}/#{options[:css]}")
|
34
|
+
end
|
35
|
+
else
|
36
|
+
if File.exists?("#{file_path}.css") || File.exists?("#{file_path}.css.scss") || File.exists?("#{file_path}.css.sass") || File.exists?("#{file_path}.scss") || File.exists?("#{file_path}.sass")
|
37
|
+
out << stylesheet_link_tag("#{directory}#{filename}/#{filename}")
|
38
|
+
end
|
39
|
+
end
|
20
40
|
end
|
21
41
|
|
22
42
|
# Render the section partial into the view.
|
23
43
|
partial_path = "#{directory_path}/_#{filename}.html"
|
24
|
-
if
|
25
|
-
|
44
|
+
if options.has_key? :partial
|
45
|
+
if options[:partial] == :tag
|
46
|
+
# :partial => :tag given --> render the tag.
|
47
|
+
out << content_tag(:div, '', :class => filename)
|
48
|
+
elsif options[:partial]
|
49
|
+
# some value for :partial given --> render the given partial.
|
50
|
+
out << render(:partial => "/../sections/#{directory}#{filename}/#{filename}")
|
51
|
+
else
|
52
|
+
# :partial => false given --> render nothing
|
53
|
+
end
|
26
54
|
else
|
27
|
-
|
55
|
+
# No :partial option given --> render the file or tag per convention.
|
56
|
+
if File.exists?("#{partial_path}.erb") || File.exists?("#{partial_path}.haml")
|
57
|
+
out << render(:partial => "/../sections/#{directory}#{filename}/#{filename}")
|
58
|
+
else
|
59
|
+
out << content_tag(:div, '', :class => filename)
|
60
|
+
end
|
28
61
|
end
|
29
|
-
|
30
62
|
out.join("\n").html_safe
|
31
63
|
end
|
32
64
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sections_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05
|
12
|
+
date: 2012-06-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70129622938640 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70129622938640
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70129622938060 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70129622938060
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3
|
38
|
-
requirement: &
|
38
|
+
requirement: &70129622937560 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70129622937560
|
47
47
|
description: Sections_rails adds infrastructure to the view layer of Ruby on Rails.
|
48
48
|
It allows to define and use the HTML, CSS, and JavaScript code of dedicated sections
|
49
49
|
of pages together in one place.
|