sections_rails 0.6.6 → 0.6.7
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/lib/generators/section_generator.rb +1 -1
- data/lib/sections_rails/section.rb +31 -43
- data/lib/sections_rails/version.rb +1 -1
- data/lib/sections_rails.rb +3 -3
- data/spec/sections_rails/section_spec.rb +42 -40
- metadata +11 -11
@@ -1,49 +1,31 @@
|
|
1
1
|
module SectionsRails
|
2
2
|
require "sections_rails/config"
|
3
|
-
require "action_view"
|
4
3
|
|
5
4
|
class Section
|
6
|
-
|
7
|
-
include ActionView::Helpers::RenderingHelper
|
5
|
+
attr_reader :asset_path, :css, :directory_name, :filename, :absolute_asset_path, :js, :locals, :partial, :partial_path
|
8
6
|
|
9
|
-
|
10
|
-
attr_reader :asset_path, :css, :directory_name, :filename, :absolute_asset_path, :js, :locals, :partial, :partial_path, :path # NOTE (SZ): too many? :)
|
11
|
-
|
12
|
-
def initialize section_name, rails_obj = nil, options = {}
|
7
|
+
def initialize section_name, view = nil, options = {}
|
13
8
|
section_name = section_name.to_s
|
14
9
|
|
15
10
|
# Helpers for filenames.
|
16
|
-
@filename
|
17
|
-
@directory_name
|
18
|
-
@
|
19
|
-
@
|
20
|
-
@
|
21
|
-
@partial_path = File.join Rails.root, SectionsRails.config.path, @directory_name, @filename, "_#{@filename}"
|
11
|
+
@filename = File.basename section_name, '.*'
|
12
|
+
@directory_name = File.dirname(section_name).gsub(/^\.$/, '')
|
13
|
+
@asset_path = File.join(@directory_name, @filename, @filename).gsub(/^\//, '')
|
14
|
+
@absolute_asset_path = File.join SectionsRails.config.path, @asset_path
|
15
|
+
@partial_path = File.join(@directory_name, @filename, "_#{@filename}").gsub(/^\//, '')
|
22
16
|
|
23
17
|
# Options.
|
24
|
-
@js
|
25
|
-
@css
|
26
|
-
@partial
|
27
|
-
@locals
|
18
|
+
@js = options[:js]
|
19
|
+
@css = options[:css]
|
20
|
+
@partial = options[:partial]
|
21
|
+
@locals = options[:locals]
|
28
22
|
|
29
23
|
# For running view helper methods.
|
30
|
-
@
|
31
|
-
end
|
32
|
-
|
33
|
-
def find_existing_filename basename, extensions
|
34
|
-
extensions.each do |ext|
|
35
|
-
path = "#{basename}.#{ext}"
|
36
|
-
return path if File.exists? path
|
37
|
-
end
|
38
|
-
nil
|
39
|
-
end
|
40
|
-
|
41
|
-
# Returns the filename of the partial of this section, or nil if this section has no partial.
|
42
|
-
def find_partial_filename
|
43
|
-
find_existing_filename @partial_path, SectionsRails.config.partial_extensions
|
24
|
+
@view = view
|
44
25
|
end
|
45
26
|
|
46
27
|
# Returns the asset_path of asset with the given extensions.
|
28
|
+
# Helper method.
|
47
29
|
def find_asset_path asset_option, extensions
|
48
30
|
return nil if asset_option == false
|
49
31
|
return asset_option if asset_option
|
@@ -64,6 +46,15 @@ module SectionsRails
|
|
64
46
|
find_asset_path @js, SectionsRails.config.js_extensions
|
65
47
|
end
|
66
48
|
|
49
|
+
# Returns the filename of the partial of this section, or nil if this section has no partial.
|
50
|
+
def find_partial_filename
|
51
|
+
SectionsRails.config.partial_extensions.each do |ext|
|
52
|
+
path = File.join SectionsRails.config.path, "#{@partial_path}.#{ext}"
|
53
|
+
return path if File.exists? path
|
54
|
+
end
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
|
67
58
|
# TODO: replace this with find_asset.
|
68
59
|
def has_asset? *extensions
|
69
60
|
extensions.flatten.each do |ext|
|
@@ -85,10 +76,7 @@ module SectionsRails
|
|
85
76
|
# Returns whether this section has a template.
|
86
77
|
# Deprecated.
|
87
78
|
def has_partial?
|
88
|
-
|
89
|
-
return true if File.exists?("#{@partial_path}.#{ext}")
|
90
|
-
end
|
91
|
-
false
|
79
|
+
@view.lookup_context.template_exists? @partial_path
|
92
80
|
end
|
93
81
|
|
94
82
|
# TODO(SZ): missing specs.
|
@@ -100,27 +88,27 @@ module SectionsRails
|
|
100
88
|
|
101
89
|
# Include JS assets.
|
102
90
|
if js
|
103
|
-
result << @
|
91
|
+
result << @view.javascript_include_tag(File.join(path, js))
|
104
92
|
elsif js == false
|
105
93
|
# ":js => false" given --> don't include any JS.
|
106
94
|
elsif has_default_js_asset?
|
107
|
-
result << @
|
95
|
+
result << @view.javascript_include_tag(asset_path)
|
108
96
|
end
|
109
97
|
|
110
98
|
# Include CSS assets.
|
111
99
|
if css
|
112
|
-
result << @
|
100
|
+
result << @view.stylesheet_link_tag(File.join(path, css))
|
113
101
|
elsif css == false
|
114
102
|
# ":css => false" given --> don't include any CSS.
|
115
103
|
elsif has_default_style_asset?
|
116
|
-
result << @
|
104
|
+
result << @view.stylesheet_link_tag(@asset_path)
|
117
105
|
end
|
118
106
|
end
|
119
107
|
|
120
108
|
# Render the section partial into the view.
|
121
109
|
case partial
|
122
110
|
when :tag
|
123
|
-
result << @
|
111
|
+
result << @view.content_tag(:div, '', :class => filename)
|
124
112
|
|
125
113
|
when false
|
126
114
|
# partial: false given --> render nothing
|
@@ -129,15 +117,15 @@ module SectionsRails
|
|
129
117
|
# partial: nil given --> render default partial
|
130
118
|
|
131
119
|
if self.has_partial?
|
132
|
-
result << @
|
120
|
+
result << @view.render(:partial => asset_path, :locals => locals)
|
133
121
|
else
|
134
|
-
result << @
|
122
|
+
result << @view.content_tag(:div, '', :class => filename)
|
135
123
|
end
|
136
124
|
|
137
125
|
else
|
138
126
|
# partial: custom path given --> render custom partial
|
139
127
|
|
140
|
-
result << @
|
128
|
+
result << @view.render("#{path}/#{partial}", locals)
|
141
129
|
end
|
142
130
|
|
143
131
|
result.join("\n").html_safe
|
data/lib/sections_rails.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'sections_rails/helpers'
|
2
|
-
require "sections_rails/section"
|
3
|
-
|
4
1
|
module SectionsRails
|
2
|
+
require 'action_view'
|
3
|
+
require 'sections_rails/helpers'
|
4
|
+
require "sections_rails/section"
|
5
5
|
require "sections_rails/railtie" if defined?(Rails)
|
6
6
|
|
7
7
|
def section name, options = {}
|
@@ -8,54 +8,34 @@ describe SectionsRails::Section do
|
|
8
8
|
describe 'initialize' do
|
9
9
|
|
10
10
|
context 'with section in folder' do
|
11
|
-
its(:filename)
|
12
|
-
its(:directory_name)
|
13
|
-
its(:asset_path)
|
14
|
-
its(:absolute_asset_path)
|
15
|
-
its(:partial_path)
|
11
|
+
its(:filename) { should == 'section' }
|
12
|
+
its(:directory_name) { should == 'folder' }
|
13
|
+
its(:asset_path) { should == 'folder/section/section' }
|
14
|
+
its(:absolute_asset_path) { should == 'app/sections/folder/section/section' }
|
15
|
+
its(:partial_path) { should == 'folder/section/_section' }
|
16
16
|
end
|
17
17
|
|
18
18
|
context 'without folder' do
|
19
19
|
subject { SectionsRails::Section.new 'section', nil }
|
20
|
-
its(:filename)
|
21
|
-
its(:directory_name)
|
22
|
-
its(:asset_path)
|
23
|
-
its(:absolute_asset_path)
|
24
|
-
its(:partial_path)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe 'find_partial_filename' do
|
29
|
-
|
30
|
-
it 'looks for all known types of partials' do
|
31
|
-
File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/_section.html.erb").and_return(false)
|
32
|
-
File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/_section.html.haml").and_return(false)
|
33
|
-
subject.find_partial_filename
|
34
|
-
end
|
35
|
-
|
36
|
-
it "returns nil if it doesn't find any assets" do
|
37
|
-
File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/_section.html.erb").and_return(false)
|
38
|
-
File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/_section.html.haml").and_return(false)
|
39
|
-
subject.find_partial_filename.should be_false
|
40
|
-
end
|
41
|
-
|
42
|
-
it "returns the absolute path to the asset if it finds one" do
|
43
|
-
File.stub(:exists?).and_return(true)
|
44
|
-
subject.find_partial_filename.should == '/rails_root/app/sections/folder/section/_section.html.erb'
|
20
|
+
its(:filename) { should == 'section' }
|
21
|
+
its(:directory_name) { should == '' }
|
22
|
+
its(:asset_path) { should == 'section/section' }
|
23
|
+
its(:absolute_asset_path) { should == 'app/sections/section/section' }
|
24
|
+
its(:partial_path) { should == 'section/_section' }
|
45
25
|
end
|
46
26
|
end
|
47
27
|
|
48
28
|
describe 'find_js_asset_path' do
|
49
29
|
it 'tries all different JS asset file types for sections' do
|
50
30
|
SectionsRails.config.js_extensions.each do |ext|
|
51
|
-
File.should_receive(:exists?).with("
|
31
|
+
File.should_receive(:exists?).with("app/sections/folder/section/section.#{ext}").and_return(false)
|
52
32
|
end
|
53
33
|
subject.find_js_asset_path
|
54
34
|
end
|
55
35
|
|
56
36
|
it 'returns nil if there is no known JS asset file' do
|
57
37
|
SectionsRails.config.js_extensions.each do |ext|
|
58
|
-
File.should_receive(:exists?).with("
|
38
|
+
File.should_receive(:exists?).with("app/sections/folder/section/section.#{ext}").and_return(false)
|
59
39
|
end
|
60
40
|
subject.find_js_asset_path.should be_false
|
61
41
|
end
|
@@ -77,31 +57,53 @@ describe SectionsRails::Section do
|
|
77
57
|
end
|
78
58
|
end
|
79
59
|
|
60
|
+
|
61
|
+
describe 'find_partial_filename' do
|
62
|
+
|
63
|
+
it 'looks for all known types of partials' do
|
64
|
+
File.should_receive(:exists?).with("app/sections/folder/section/_section.html.erb").and_return(false)
|
65
|
+
File.should_receive(:exists?).with("app/sections/folder/section/_section.html.haml").and_return(false)
|
66
|
+
subject.find_partial_filename
|
67
|
+
end
|
68
|
+
|
69
|
+
it "returns nil if it doesn't find any assets" do
|
70
|
+
File.should_receive(:exists?).with("app/sections/folder/section/_section.html.erb").and_return(false)
|
71
|
+
File.should_receive(:exists?).with("app/sections/folder/section/_section.html.haml").and_return(false)
|
72
|
+
subject.find_partial_filename.should be_false
|
73
|
+
end
|
74
|
+
|
75
|
+
it "returns the absolute path to the asset if it finds one" do
|
76
|
+
File.stub(:exists?).and_return(true)
|
77
|
+
subject.find_partial_filename.should == 'app/sections/folder/section/_section.html.erb'
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
80
82
|
describe "#has_asset?" do
|
81
83
|
|
82
84
|
it "tries filename variations with all given extensions" do
|
83
|
-
File.should_receive(:exists?).with("
|
84
|
-
File.should_receive(:exists?).with("
|
85
|
+
File.should_receive(:exists?).with("app/sections/folder/section/section.one").and_return(false)
|
86
|
+
File.should_receive(:exists?).with("app/sections/folder/section/section.two").and_return(false)
|
85
87
|
subject.has_asset? ['one', 'two']
|
86
88
|
end
|
87
89
|
|
88
90
|
it "returns false if the files don't exist" do
|
89
|
-
File.should_receive(:exists?).with("
|
91
|
+
File.should_receive(:exists?).with("app/sections/folder/section/section.one").and_return(false)
|
90
92
|
subject.has_asset?(['one']).should be_false
|
91
93
|
end
|
92
94
|
|
93
95
|
it "returns true if one of the given extensions matches a file" do
|
94
|
-
File.should_receive(:exists?).with("
|
95
|
-
File.should_receive(:exists?).with("
|
96
|
+
File.should_receive(:exists?).with("app/sections/folder/section/section.one").and_return(false)
|
97
|
+
File.should_receive(:exists?).with("app/sections/folder/section/section.two").and_return(true)
|
96
98
|
subject.has_asset?(['one', 'two']).should be_true
|
97
99
|
end
|
98
100
|
end
|
99
101
|
|
100
102
|
describe "#has_default_js_asset" do
|
101
103
|
it 'looks for all different types of JS file types' do
|
102
|
-
File.should_receive(:exists?).with("
|
103
|
-
File.should_receive(:exists?).with("
|
104
|
-
File.should_receive(:exists?).with("
|
104
|
+
File.should_receive(:exists?).with("app/sections/folder/section/section.js").and_return(false)
|
105
|
+
File.should_receive(:exists?).with("app/sections/folder/section/section.coffee").and_return(false)
|
106
|
+
File.should_receive(:exists?).with("app/sections/folder/section/section.js.coffee").and_return(false)
|
105
107
|
subject.has_default_js_asset?.should be_false
|
106
108
|
end
|
107
109
|
|
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.6.
|
4
|
+
version: 0.6.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-06-08 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70275409137460 !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: *70275409137460
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70275409136460 !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: *70275409136460
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3
|
38
|
-
requirement: &
|
38
|
+
requirement: &70275409135640 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70275409135640
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: guard-rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &70275409134400 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70275409134400
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rb-fsevent
|
60
|
-
requirement: &
|
60
|
+
requirement: &70275409133660 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70275409133660
|
69
69
|
description: Sections_rails adds infrastructure to the view layer of Ruby on Rails.
|
70
70
|
It allows to define and use the HTML, CSS, and JavaScript code of dedicated sections
|
71
71
|
of pages together in one place.
|