sections_rails 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,7 @@
1
1
  # Generates a new section.
2
2
  class SectionGenerator < Rails::Generators::Base
3
+ include SectionsRails::Helpers
4
+
3
5
  argument :name, :type => :string
4
6
 
5
7
  def create_folder
@@ -7,44 +9,36 @@ class SectionGenerator < Rails::Generators::Base
7
9
  end
8
10
 
9
11
  def create_partial
10
- directory, filename = split_path
11
- create_file "#{asset_base_path '_'}.html.haml",
12
+ filename = File.basename(name)
13
+ create_file "_#{asset_base_path '_'}.html.haml",
12
14
  ".#{filename}\n -# DOM content goes here.\n"
13
15
  end
14
16
 
15
17
  def create_coffee_file
16
- directory, filename = split_path
18
+ filename = File.basename(name)
17
19
  create_file "#{asset_base_path}.coffee",
18
20
  "class #{filename}\n # Your CoffeeScript code goes here.\n"
19
21
  end
20
22
 
21
23
  def create_sass_file
22
- directory, filename = split_path
24
+ filename = File.basename(name)
23
25
  create_file "#{asset_base_path}.sass",
24
26
  ".#{filename}\n /* Your SASS goes here. */\n"
25
27
  end
26
28
 
27
-
28
29
  private
29
30
 
30
- # Returns an array [directory, filename] of the given filename.
31
- def split_path
32
- split_names = name.split '/'
33
- filename = split_names[-1]
34
- directory = split_names[0..-2].join '/'
35
- directory += '/' if directory.size > 0
36
- [directory, filename]
37
- end
38
-
39
31
  # Returns the path for the directory of the section.
40
32
  def directory_path
41
- directory, filename = split_path
42
- "app/sections/#{directory}#{filename}"
33
+ @directory_path ||= begin
34
+ directory, filename = split_path(name)
35
+ "app/sections/#{directory}/#{filename}"
36
+ end
43
37
  end
44
38
 
45
39
  # Returns the base path of the file, i.e. '/app/sections/admin/foo/foo'.
46
40
  def asset_base_path file_prefix = nil
47
- directory, filename = split_path
41
+ filename = File.basename(name)
48
42
  "#{directory_path}/#{file_prefix}#{filename}"
49
43
  end
50
44
  end
@@ -0,0 +1,35 @@
1
+ # Configuration options for SectionsRails.
2
+ # Set in an initializer file in /config/initializers/.
3
+ #
4
+ # Example:
5
+ # SectionsRails.configure do |config|
6
+ # config.spec_dir = "spec/javascripts"
7
+ # config.driver = :webkit
8
+ # end if defined?(Konacha)
9
+ module SectionsRails
10
+
11
+ class << self
12
+ def config
13
+ @config ||= Config.new
14
+ end
15
+ end
16
+
17
+ class Config
18
+ attr_accessor :path, :js_extensions, :css_extensions, :partial_extensions
19
+
20
+ def initialize options = {}
21
+ options.reverse_merge!({ :path => 'app/sections',
22
+ :js_extensions => ['js', 'js.coffee', 'coffee'],
23
+ :css_extensions => ['css', 'css.scss', 'css.sass', 'sass', 'scss'],
24
+ :partial_extensions => ['html.erb', 'html.haml'] })
25
+
26
+ options.each do |option, value|
27
+ if self.respond_to? option
28
+ send("#{option}=", value)
29
+ else
30
+ raise ArgumentError.new "Invalid option '#{option}' for #{self.class.name}"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ module SectionsRails
2
+ module Helpers
3
+
4
+ # Returns a list of all section names in the given text.
5
+ #
6
+ # @param [ String ] text
7
+ # @return [ Array<String> ]
8
+ def find_sections text
9
+
10
+ # Find sections in ERB templates.
11
+ result = text.scan(/<%=\s*section\s+['":]([^'",\s]+)/).flatten.sort.uniq
12
+
13
+ # Find sections in HAML templates.
14
+ result.concat text.scan(/^\s*\=\s*section\s+['":]([^'",\s]+)/).flatten.sort.uniq
15
+
16
+ result
17
+ end
18
+
19
+ # Returns directory and filename portion of the given path.
20
+ #
21
+ # @param [ String ] paths
22
+ # @return [ Array<String, String> ]
23
+ def split_path paths
24
+ dirname = File.dirname(paths)
25
+ dirname = '' if dirname == '.'
26
+ [dirname, File.basename(paths)]
27
+ end
28
+ end
29
+ end
@@ -5,5 +5,9 @@ module SectionsRails
5
5
  rake_tasks do
6
6
  load "tasks/sections_rails_tasks.rake"
7
7
  end
8
+
9
+ config.after_initialize do
10
+ ActionController::Base.view_paths << SectionsRails.config.path
11
+ end
8
12
  end
9
13
  end
@@ -0,0 +1,106 @@
1
+ module SectionsRails
2
+ require "sections_rails/config"
3
+ require "action_view"
4
+
5
+ class Section
6
+ include ActionView::Helpers::AssetTagHelper
7
+ include ActionView::Helpers::RenderingHelper
8
+
9
+ # TODO(KG): remove unnessessary.
10
+ attr_reader :asset_path, :css, :directory_name, :filename, :absolute_path, :js, :locals, :partial, :partial_path, :path # NOTE (SZ): too many? :)
11
+
12
+ def initialize section_name, rails_obj, options = {}
13
+
14
+ # Helpers for filenames.
15
+ @filename = File.basename(section_name, '.*')
16
+ @directory_name = File.dirname(section_name)
17
+ @asset_path = File.join(@directory_name, @filename, @filename)
18
+ @absolute_path = File.join(Rails.root, SectionsRails.config.path, @directory_name, @filename, @filename)
19
+ @partial_path = File.join(Rails.root, SectionsRails.config.path, @directory_name, @filename, "_#{@filename}")
20
+
21
+ # Options.
22
+ @js = options[:js]
23
+ @css = options[:css]
24
+ @partial = options[:partial]
25
+ @locals = options[:locals]
26
+
27
+ # For running view helper methods.
28
+ @rails_obj = rails_obj
29
+ end
30
+
31
+ def has_asset? *extensions
32
+ extensions.flatten.each do |ext|
33
+ return true if File.exists?("#{@absolute_path}.#{ext}")
34
+ end
35
+ false
36
+ end
37
+
38
+ def has_default_js_asset?
39
+ has_asset? SectionsRails.config.js_extensions
40
+ end
41
+
42
+ def has_default_style_asset?
43
+ has_asset? SectionsRails.config.css_extensions
44
+ end
45
+
46
+ # Returns whether this section has a template.
47
+ def has_template?
48
+ SectionsRails.config.partial_extensions.each do |ext|
49
+ return true if File.exists?("#{@partial_path}.#{ext}")
50
+ end
51
+ false
52
+ end
53
+
54
+ # TODO(SZ): missing specs.
55
+ def render
56
+ result = []
57
+
58
+ # Include assets only for development mode.
59
+ if Rails.env != 'production'
60
+
61
+ # Include JS assets.
62
+ if js
63
+ result << @rails_obj.javascript_include_tag(File.join(path, js))
64
+ elsif js == false
65
+ # ":js => false" given --> don't include any JS.
66
+ elsif has_default_js_asset?
67
+ result << @rails_obj.javascript_include_tag(asset_path)
68
+ end
69
+
70
+ # Include CSS assets.
71
+ if css
72
+ result << @rails_obj.stylesheet_link_tag(File.join(path, css))
73
+ elsif css == false
74
+ # ":css => false" given --> don't include any CSS.
75
+ elsif has_default_style_asset?
76
+ result << @rails_obj.stylesheet_link_tag(@asset_path)
77
+ end
78
+ end
79
+
80
+ # Render the section partial into the view.
81
+ case partial
82
+ when :tag
83
+ result << @rails_obj.content_tag(:div, '', :class => filename)
84
+
85
+ when false
86
+ # partial: false given --> render nothing
87
+
88
+ when nil
89
+ # partial: nil given --> render default partial
90
+
91
+ if self.has_template?
92
+ result << @rails_obj.render(:partial => asset_path, :locals => locals)
93
+ else
94
+ result << @rails_obj.content_tag(:div, '', :class => filename)
95
+ end
96
+
97
+ else
98
+ # partial: custom path given --> render custom partial
99
+
100
+ result << @rails_obj.render("#{path}/#{partial}", locals)
101
+ end
102
+
103
+ result.join("\n").html_safe
104
+ end
105
+ end
106
+ end
@@ -1,3 +1,3 @@
1
1
  module SectionsRails
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.2"
3
3
  end
@@ -1,74 +1,12 @@
1
- require 'sections_rails/string_tools'
1
+ require 'sections_rails/helpers'
2
+ require "sections_rails/section"
2
3
 
3
4
  module SectionsRails
4
-
5
5
  require "sections_rails/railtie" if defined?(Rails)
6
-
7
- def section combined_name, options = {}
8
- result = []
9
-
10
- # Split the parameter into file name and directory name.
11
- directory, filename = split_path combined_name
12
- directory_path = "#{Rails.root}/app/sections/#{directory}#{filename}" # Directory of section: /app/sections/admin/logo
13
-
14
- # Add assets of section when in dev mode.
15
- file_path = "#{directory_path}/#{filename}" # Base path of filename in section: /app/sections/admin/logo/logo
16
- if Rails.env != 'production'
17
-
18
- # Include JS assets.
19
- if options.has_key? :js
20
- if 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.
25
- end
26
- else
27
- # No :js option given --> load the default JS file.
28
- if File.exists?("#{file_path}.js") || File.exists?("#{file_path}.js.coffee") || File.exists?("#{file_path}.coffee")
29
- result << javascript_include_tag("#{directory}#{filename}/#{filename}")
30
- end
31
- end
32
-
33
- # Include CSS assets.
34
- if options.has_key? :css
35
- if 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.
40
- end
41
- else
42
- # No :css option given --> render the default :css file.
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")
44
- result << stylesheet_link_tag("#{directory}#{filename}/#{filename}")
45
- end
46
- end
47
- end
48
6
 
49
- # Render the section partial into the view.
50
- partial_path = "#{directory_path}/_#{filename}.html"
51
- if options.has_key? :partial
52
- if options[:partial] == :tag
53
- # :partial => :tag given --> render the tag.
54
- result << content_tag(:div, '', :class => filename)
55
- elsif options[:partial]
56
- # some value for :partial given --> render the given partial.
57
- result << render(:partial => "/../sections/#{directory}#{filename}/#{options[:partial]}", :locals => options[:locals])
58
- else
59
- # partial: false or nil given --> render nothing
60
- end
61
- else
62
- # No :partial option given --> render the file or tag per convention.
63
- if File.exists?("#{partial_path}.erb") || File.exists?("#{partial_path}.haml")
64
- result << render(:partial => "/../sections/#{directory}#{filename}/#{filename}", :locals => options[:locals])
65
- else
66
- result << content_tag(:div, '', :class => filename)
67
- end
68
- end
69
- result.join("\n").html_safe
7
+ def section name, options = {}
8
+ SectionsRails::Section.new(name, self, options).render
70
9
  end
71
-
72
10
  end
73
11
 
74
12
  ActionView::Base.send :include, SectionsRails
@@ -1,4 +1,5 @@
1
- namespace :sections do
1
+ namespace :sections do
2
+ include SectionsRails::Helpers
2
3
 
3
4
  desc "Prepares the assets for precompilation in a setup with a single application.js file"
4
5
  task :prepare do
@@ -1,9 +1,9 @@
1
1
  require 'spec_helper'
2
- require 'sections_rails/string_tools'
2
+ include SectionsRails::Helpers
3
3
 
4
- describe 'string_tools' do
4
+ describe SectionsRails::Helpers do
5
5
 
6
- describe 'find_sections' do
6
+ describe '#find_sections' do
7
7
  it 'finds ERB sections with symbols' do
8
8
  find_sections("one <%= section :alpha %> two").should == ['alpha']
9
9
  end
@@ -53,10 +53,10 @@ describe 'string_tools' do
53
53
  end
54
54
  end
55
55
 
56
- describe 'split_path' do
56
+ describe '#split_path' do
57
57
  it 'returns the directory and filename of the given path' do
58
58
  directory, filename = split_path '/one/two/three.js'
59
- directory.should == '/one/two/'
59
+ directory.should == '/one/two'
60
60
  filename.should == 'three.js'
61
61
  end
62
62
 
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ # TODO(SZ): missing specs.
4
+ describe SectionsRails::Section do
5
+ before(:each) { Rails.stub(:root).and_return('/rails_root/') }
6
+ subject { SectionsRails::Section.new 'folder/section' }
7
+
8
+ describe 'initialize' do
9
+ its(:filename) { should == 'section' }
10
+ its(:directory_name) { should == 'folder' }
11
+ its(:asset_path) { should == 'folder/section/section' }
12
+ its(:absolute_path) { should == '/rails_root/app/sections/folder/section/section' }
13
+ its(:partial_path) { should == '/rails_root/app/sections/folder/section/_section' }
14
+ end
15
+
16
+ describe "#has_asset?" do
17
+
18
+ it "tries filename variations with all given extensions" do
19
+ File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/section.one").and_return(false)
20
+ File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/section.two").and_return(false)
21
+ subject.has_asset? ['one', 'two']
22
+ end
23
+
24
+ it "returns false if the files don't exist" do
25
+ File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/section.one").and_return(false)
26
+ subject.has_asset?(['one']).should be_false
27
+ end
28
+
29
+ it "returns true if one of the given extensions matches a file" do
30
+ File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/section.one").and_return(false)
31
+ File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/section.two").and_return(true)
32
+ subject.has_asset?(['one', 'two']).should be_true
33
+ end
34
+ end
35
+
36
+ describe "#has_default_js_asset" do
37
+ it 'looks for all different types of JS file types' do
38
+ File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/section.js").and_return(false)
39
+ File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/section.coffee").and_return(false)
40
+ File.should_receive(:exists?).with("/rails_root/app/sections/folder/section/section.js.coffee").and_return(false)
41
+ subject.has_default_js_asset?.should be_false
42
+ end
43
+
44
+ it 'returns TRUE if it JS file types' do
45
+ File.stub!(:exists?).and_return(true)
46
+ subject.has_default_js_asset?.should be_true
47
+ end
48
+ end
49
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
+ require 'rails'
3
4
  require 'rspec'
5
+ require 'rspec-rails'
6
+ require 'sections_rails'
4
7
 
5
8
  # Configure Rails Environment
6
9
  ENV["RAILS_ENV"] = "test"
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.1
4
+ version: 0.6.2
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-07 00:00:00.000000000 Z
12
+ date: 2012-06-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70244820224220 !ruby/object:Gem::Requirement
16
+ requirement: &70157100841340 !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: *70244820224220
24
+ version_requirements: *70157100841340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70244820223480 !ruby/object:Gem::Requirement
27
+ requirement: &70157100840720 !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: *70244820223480
35
+ version_requirements: *70157100840720
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70244820222620 !ruby/object:Gem::Requirement
38
+ requirement: &70157100839900 !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: *70244820222620
46
+ version_requirements: *70157100839900
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: guard-rspec
49
- requirement: &70244820221720 !ruby/object:Gem::Requirement
49
+ requirement: &70157100838960 !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: *70244820221720
57
+ version_requirements: *70157100838960
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rb-fsevent
60
- requirement: &70244820220540 !ruby/object:Gem::Requirement
60
+ requirement: &70157100837520 !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: *70244820220540
68
+ version_requirements: *70157100837520
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.
@@ -77,15 +77,18 @@ extra_rdoc_files: []
77
77
  files:
78
78
  - lib/generators/section_generator.rb
79
79
  - lib/generators/sections_generator.rb
80
+ - lib/sections_rails/config.rb
81
+ - lib/sections_rails/helpers.rb
80
82
  - lib/sections_rails/railtie.rb
81
- - lib/sections_rails/string_tools.rb
83
+ - lib/sections_rails/section.rb
82
84
  - lib/sections_rails/version.rb
83
85
  - lib/sections_rails.rb
84
86
  - lib/tasks/sections_rails_tasks.rake
85
87
  - MIT-LICENSE
86
88
  - Rakefile
87
89
  - README.md
88
- - spec/lib/string_tools_spec.rb
90
+ - spec/sections_rails/helpers_spec.rb
91
+ - spec/sections_rails/section_spec.rb
89
92
  - spec/spec_helper.rb
90
93
  homepage: https://github.com/kevgo/sections_rails
91
94
  licenses: []
@@ -113,5 +116,6 @@ specification_version: 3
113
116
  summary: A rails plugin that allows to define the HTML, CSS, and JS for individual
114
117
  sections of pages as one unit.
115
118
  test_files:
116
- - spec/lib/string_tools_spec.rb
119
+ - spec/sections_rails/helpers_spec.rb
120
+ - spec/sections_rails/section_spec.rb
117
121
  - spec/spec_helper.rb
@@ -1,15 +0,0 @@
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