sections_rails 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -97,12 +97,18 @@ In this case, the _sections_ helper creates an empty div in the view.
97
97
  ## Options
98
98
 
99
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.
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_.
102
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_.
103
+ ```erb
104
+ <%= section :hello_world, partial: 'hello_new', css: false, js: 'foobar.js' %>
105
+ ```
105
106
 
107
+ It is also possible to provide parameters to the rendered partial.
108
+
109
+ ```erb
110
+ <%= section :hello_world, locals: { message: 'Greetings!' } %>
111
+ ```
106
112
 
107
113
  ## Creating new sections.
108
114
 
@@ -127,6 +133,20 @@ _This feature is still under development._
127
133
  To test them for example using [Konacha](https://github.com/jfirebaugh/konacha), create a symlink to _app/sections_ in _spec/javascript_.
128
134
 
129
135
 
136
+ # Development
137
+
138
+ ## Unit tests
139
+
140
+ ```bash
141
+ $ rake
142
+ ```
143
+
144
+ To automatically run unit tests when files change, run
145
+
146
+ ```bash
147
+ $ bundle exec guard -c
148
+ ```
149
+
130
150
  # Missing features
131
151
 
132
152
  _Sections_rails_ is in prototypical development and far from complete. Missing features are:
data/Rakefile CHANGED
@@ -22,18 +22,6 @@ end
22
22
 
23
23
  Bundler::GemHelper.install_tasks
24
24
 
25
- desc 'Default: run the specs and features.'
26
- task :default => 'spec:unit' do
27
- system("bundle exec rake spec")
28
- end
29
-
30
- namespace :spec do
31
-
32
- desc "Run unit specs"
33
- RSpec::Core::RakeTask.new('unit') do |t|
34
- t.pattern = 'spec/{*_spec.rb}'
35
- end
36
- end
37
-
38
- desc "Run the unit tests"
39
- task :spec => ['spec:unit']
25
+ require 'rspec/core/rake_task'
26
+ RSpec::Core::RakeTask.new :spec
27
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ # Returns a list of all section names in the given text.
2
+ def find_sections text
3
+ result = text.scan(/<%=\s*section\s+['":]([^'",\s]+)/).flatten.sort.uniq
4
+ result.concat text.scan(/^\s*\=\s*section\s+['":]([^'",\s]+)/).flatten.sort.uniq
5
+ result
6
+ end
7
+
8
+
9
+ # Returns directory and filename portion of the given path.
10
+ def split_path paths
11
+ segments = paths.split '/'
12
+ directory = segments[0..-2].join('/')
13
+ directory += '/' if directory.size > 0
14
+ [directory, segments[-1]]
15
+ end
@@ -1,3 +1,3 @@
1
1
  module SectionsRails
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -1,15 +1,14 @@
1
+ require 'sections_rails/string_tools'
2
+
1
3
  module SectionsRails
2
4
 
3
5
  require "sections_rails/railtie" if defined?(Rails)
4
6
 
5
7
  def section combined_name, options = {}
6
- out = []
8
+ result = []
7
9
 
8
10
  # Split the parameter into file name and directory name.
9
- split_names = combined_name.to_s.split '/'
10
- filename = split_names[-1]
11
- directory = (split_names.size > 1 ? split_names[0..-2] : []).join '/'
12
- directory += '/' if directory.size > 0
11
+ directory, filename = split_path combined_name
13
12
  directory_path = "#{Rails.root}/app/sections/#{directory}#{filename}" # Directory of section: /app/sections/admin/logo
14
13
 
15
14
  # Add assets of section when in dev mode.
@@ -19,22 +18,30 @@ module SectionsRails
19
18
  # Include JS assets.
20
19
  if options.has_key? :js
21
20
  if options[:js]
22
- out << javascript_include_tag("#{directory}#{filename}/#{options[:js]}")
21
+ # Custom :js filename given --> load the given JS file.
22
+ result << javascript_include_tag("#{directory}#{filename}/#{options[:js]}")
23
+ else
24
+ # js: false given --> don't do anything here.
23
25
  end
24
26
  else
27
+ # No :js option given --> load the default JS file.
25
28
  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}")
29
+ result << javascript_include_tag("#{directory}#{filename}/#{filename}")
27
30
  end
28
31
  end
29
32
 
30
33
  # Include CSS assets.
31
34
  if options.has_key? :css
32
35
  if options[:css]
33
- out << stylesheet_link_tag("#{directory}#{filename}/#{options[:css]}")
36
+ # Custom :css option given --> render the given file.
37
+ result << stylesheet_link_tag("#{directory}#{filename}/#{options[:css]}")
38
+ else
39
+ # css: false given --> don't render any css.
34
40
  end
35
41
  else
42
+ # No :css option given --> render the default :css file.
36
43
  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}")
44
+ result << stylesheet_link_tag("#{directory}#{filename}/#{filename}")
38
45
  end
39
46
  end
40
47
  end
@@ -44,22 +51,22 @@ module SectionsRails
44
51
  if options.has_key? :partial
45
52
  if options[:partial] == :tag
46
53
  # :partial => :tag given --> render the tag.
47
- out << content_tag(:div, '', :class => filename)
54
+ result << content_tag(:div, '', :class => filename)
48
55
  elsif options[:partial]
49
56
  # some value for :partial given --> render the given partial.
50
- out << render(:partial => "/../sections/#{directory}#{filename}/#{filename}", :locals => options[:locals])
57
+ result << render(:partial => "/../sections/#{directory}#{filename}/#{options[:partial]}", :locals => options[:locals])
51
58
  else
52
- # :partial => false or nil given --> render nothing
59
+ # partial: false or nil given --> render nothing
53
60
  end
54
61
  else
55
62
  # No :partial option given --> render the file or tag per convention.
56
63
  if File.exists?("#{partial_path}.erb") || File.exists?("#{partial_path}.haml")
57
- out << render(:partial => "/../sections/#{directory}#{filename}/#{filename}", :locals => options[:locals])
64
+ result << render(:partial => "/../sections/#{directory}#{filename}/#{filename}", :locals => options[:locals])
58
65
  else
59
- out << content_tag(:div, '', :class => filename)
66
+ result << content_tag(:div, '', :class => filename)
60
67
  end
61
68
  end
62
- out.join("\n").html_safe
69
+ result.join("\n").html_safe
63
70
  end
64
71
 
65
72
  end
@@ -4,18 +4,26 @@ namespace :sections do
4
4
  task :prepare do
5
5
  puts "\nPreparing sections assets ..."
6
6
 
7
- # Find all views in this app.
8
- views = find_all_views 'app/views'
9
-
10
- # Get all sections used in the views.
11
- sections = views.map do |view|
12
- find_sections_in_view IO.read "app/views/#{view}"
7
+ # Find all sections used in the views.
8
+ sections = find_all_views('app/views').map do |view|
9
+ find_sections IO.read"app/views/#{view}"
13
10
  end.flatten
14
11
 
12
+ # Find all sections within the already known sections.
13
+ i = 0
14
+ while i < sections.size
15
+ section = sections[i]
16
+ referenced_sections = find_sections_in_section section
17
+ referenced_sections.each do |referenced_section|
18
+ sections << referenced_section unless sections.include? referenced_section
19
+ end
20
+ i += 1
21
+ end
22
+ sections.sort!
23
+
15
24
  # Create the require file for application.js.
16
25
  File.open "app/assets/javascripts/application_sections.js", 'w' do |file|
17
26
  sections.each do |section|
18
-
19
27
  if File.exists?(asset_path section, '.js') || File.exists?(asset_path section, '.js.coffee') || File.exists?(asset_path section, '.coffee')
20
28
  file.write "//= require #{require_path section}\n"
21
29
  end
@@ -100,42 +108,32 @@ namespace :sections do
100
108
  result
101
109
  end
102
110
 
111
+ # Used for :prepare_pages. Not used right now.
103
112
  def parse_views views
104
113
  result = {}
105
114
  views_to_parse.each do |view_to_parse|
106
115
  view_text = IO.read(File.join root, view_to_parse)
107
- result[view_to_parse] = find_sections_in_view view_text
116
+ result[view_to_parse] = find_sections_in_text view_text
108
117
  end
109
118
  result
110
119
  end
111
120
 
112
- # Returns an array with the name of all sections in the given view source.
113
- def find_sections_in_view view_text
114
- erb_sections = view_text.scan(/<%=\s*section\s+['":](.*?)['"]?\s*%>/).flatten.sort.uniq
115
- haml_sections = view_text.scan(/\=\s*section\s+['":](.*?)['"]?$/).flatten.sort.uniq
116
- erb_sections + haml_sections
117
- end
118
-
119
- # Returns whether the given file contains the given text somewhere in its content.
120
- def file_contains file_name, text
121
- IO.read(file_name) =~ text
121
+
122
+ def find_sections_in_section section_name
123
+ directory, filename = split_path section_name
124
+ section_content = IO.read "app/sections/#{directory}#{filename}/_#{filename}.html.haml"
125
+ find_sections section_content
122
126
  end
123
-
127
+
124
128
  # Returns the path to the asset in the given section.
125
129
  def asset_path section_name, asset_extension = nil, asset_prefix = nil
126
- split_names = section_name.split '/'
127
- filename = split_names[-1]
128
- directory = split_names[0..-2].join '/'
129
- directory += '/' if directory.size > 0
130
+ directory, filename = split_path section_name
130
131
  "app/sections/#{directory}#{filename}/#{asset_prefix}#{filename}#{asset_extension}"
131
132
  end
132
133
 
133
134
  # Returns the relative path to the asset for the asset pipeline.
134
135
  def require_path section_name
135
- split_names = section_name.split '/'
136
- filename = split_names[-1]
137
- directory = split_names[0..-2].join '/'
138
- directory += '/' if directory.size > 0
136
+ directory, filename = split_path section_name
139
137
  "../../sections/#{directory}#{filename}/#{filename}"
140
138
  end
141
139
  end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+ require 'sections_rails/string_tools'
3
+
4
+ describe 'string_tools' do
5
+
6
+ describe 'find_sections' do
7
+ it 'finds ERB sections with symbols' do
8
+ find_sections("one <%= section :alpha %> two").should == ['alpha']
9
+ end
10
+
11
+ it 'finds ERB sections with single quotes' do
12
+ find_sections("one <%= section 'alpha' %> two").should == ['alpha']
13
+ end
14
+
15
+ it 'finds ERB sections with double quotes' do
16
+ find_sections('one <%= section "alpha" %> two').should == ['alpha']
17
+ end
18
+
19
+ it 'finds ERB sections with parameters' do
20
+ find_sections('one <%= section "alpha", css: false %> two').should == ['alpha']
21
+ end
22
+
23
+ it 'finds HAML sections with symbols' do
24
+ find_sections("= section :alpha").should == ['alpha']
25
+ end
26
+
27
+ it 'finds HAML sections with single quotes' do
28
+ find_sections("= section 'alpha'").should == ['alpha']
29
+ end
30
+
31
+ it 'finds HAML sections with double quotes' do
32
+ find_sections('= section "alpha"').should == ['alpha']
33
+ end
34
+
35
+ it 'finds indented HAML sections' do
36
+ find_sections(' = section "alpha"').should == ['alpha']
37
+ end
38
+
39
+ it 'finds HAML sections with parameters' do
40
+ find_sections('= section "alpha", css: false').should == ['alpha']
41
+ end
42
+
43
+ it 'finds all results in the text' do
44
+ find_sections("one <%= section 'alpha' \ntwo <%= section 'beta'").should == ['alpha', 'beta']
45
+ end
46
+
47
+ it 'sorts the results' do
48
+ find_sections("one <%= section 'beta' \ntwo <%= section 'alpha'").should == ['alpha', 'beta']
49
+ end
50
+
51
+ it 'removes duplicates' do
52
+ find_sections("one <%= section 'alpha' \ntwo <%= section 'alpha'").should == ['alpha']
53
+ end
54
+ end
55
+
56
+ describe 'split_path' do
57
+ it 'returns the directory and filename of the given path' do
58
+ directory, filename = split_path '/one/two/three.js'
59
+ directory.should == '/one/two/'
60
+ filename.should == 'three.js'
61
+ end
62
+
63
+ it "returns '' if there is no directory" do
64
+ directory, filename = split_path 'three.js'
65
+ directory.should == ''
66
+ filename.should == 'three.js'
67
+ end
68
+ end
69
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,15 +1,11 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
- #require "lib/sections_rails"
4
3
  require 'rspec'
5
4
 
6
5
  # Configure Rails Environment
7
6
  ENV["RAILS_ENV"] = "test"
8
7
  #Rails = Hashie::Mash.new({:env => 'test'})
9
8
 
10
- require 'sections_rails' # and any other gems you need
11
-
12
-
13
9
  # Load support files
14
10
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
15
11
 
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.0
4
+ version: 0.6.1
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-06-05 00:00:00.000000000 Z
12
+ date: 2012-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70274490604960 !ruby/object:Gem::Requirement
16
+ requirement: &70244820224220 !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: *70274490604960
24
+ version_requirements: *70244820224220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70274490604500 !ruby/object:Gem::Requirement
27
+ requirement: &70244820223480 !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: *70274490604500
35
+ version_requirements: *70244820223480
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70274490600400 !ruby/object:Gem::Requirement
38
+ requirement: &70244820222620 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,29 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70274490600400
46
+ version_requirements: *70244820222620
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-rspec
49
+ requirement: &70244820221720 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70244820221720
58
+ - !ruby/object:Gem::Dependency
59
+ name: rb-fsevent
60
+ requirement: &70244820220540 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70244820220540
47
69
  description: Sections_rails adds infrastructure to the view layer of Ruby on Rails.
48
70
  It allows to define and use the HTML, CSS, and JavaScript code of dedicated sections
49
71
  of pages together in one place.
@@ -56,12 +78,14 @@ files:
56
78
  - lib/generators/section_generator.rb
57
79
  - lib/generators/sections_generator.rb
58
80
  - lib/sections_rails/railtie.rb
81
+ - lib/sections_rails/string_tools.rb
59
82
  - lib/sections_rails/version.rb
60
83
  - lib/sections_rails.rb
61
84
  - lib/tasks/sections_rails_tasks.rake
62
85
  - MIT-LICENSE
63
86
  - Rakefile
64
87
  - README.md
88
+ - spec/lib/string_tools_spec.rb
65
89
  - spec/spec_helper.rb
66
90
  homepage: https://github.com/kevgo/sections_rails
67
91
  licenses: []
@@ -89,4 +113,5 @@ specification_version: 3
89
113
  summary: A rails plugin that allows to define the HTML, CSS, and JS for individual
90
114
  sections of pages as one unit.
91
115
  test_files:
116
+ - spec/lib/string_tools_spec.rb
92
117
  - spec/spec_helper.rb