sections_rails 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,52 @@
1
1
  # Generates a new section.
2
2
  class SectionGenerator < Rails::Generators::Base
3
-
4
3
  argument :name, :type => :string
5
4
 
6
5
  def create_folder
7
- empty_directory "app/sections/#{name}"
6
+ empty_directory directory_path
8
7
  end
9
8
 
10
9
  def create_partial
11
- create_file "app/sections/_#{name}.html.haml",
12
- ".#{name}\n -# DOM content goes here.\n"
10
+ directory, filename = split_path
11
+ create_file "#{asset_base_path '_'}.html.haml",
12
+ ".#{filename}\n -# DOM content goes here.\n"
13
13
  end
14
14
 
15
15
  def create_coffee_file
16
- create_file "app/sections/#{name}.coffee",
17
- "# Your CoffeeScript code goes here.\n"
16
+ directory, filename = split_path
17
+ create_file "#{asset_base_path}.coffee",
18
+ "class #{filename}\n # Your CoffeeScript code goes here.\n"
18
19
  end
19
20
 
20
21
  def create_sass_file
21
- create_file "app/sections/#{name}.sass",
22
- ".#{name}\n /* Your SASS goes here. */\n"
22
+ directory, filename = split_path
23
+ create_file "#{asset_base_path}.sass",
24
+ ".#{filename}\n /* Your SASS goes here. */\n"
25
+ end
26
+
27
+
28
+ private
29
+
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.size > 1 ? split_names[0..-2] : []).join '/'
35
+ directory += '/' if directory.size > 0
36
+ p directory
37
+ p filename
38
+ [directory, filename]
39
+ end
40
+
41
+ # Returns the path for the directory of the section.
42
+ def directory_path
43
+ directory, filename = split_path
44
+ "app/sections/#{directory}#{filename}"
45
+ end
46
+
47
+ # Returns the base path of the file, i.e. '/app/sections/admin/foo/foo'.
48
+ def asset_base_path file_prefix = nil
49
+ directory, filename = split_path
50
+ "#{directory_path}/#{file_prefix}#{filename}"
23
51
  end
24
52
  end
@@ -1,3 +1,3 @@
1
1
  module SectionsRails
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -2,22 +2,31 @@ module SectionsRails
2
2
 
3
3
  require "sections_rails/railtie" if defined?(Rails)
4
4
 
5
- def section name
5
+ def section combined_name
6
6
  out = []
7
- filename = "#{Rails.root}/app/sections/#{name}/#{name}"
7
+
8
+ # 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
13
+
14
+ directory_path = "#{Rails.root}/app/sections/#{directory}#{filename}" # Directory of section: /app/sections/admin/logo
15
+ file_path = "#{directory_path}/#{filename}" # Base path of filename in section: /app/sections/admin/logo/logo
16
+
8
17
 
9
18
  # Add assets of section when in dev mode.
10
19
  if Rails.env != 'production'
11
- out << javascript_include_tag("#{name}/#{name}") if File.exists?("#{filename}.js") || File.exists?("#{filename}.js.coffee") || File.exists?("#{filename}.coffee")
12
- out << stylesheet_link_tag("#{name}/#{name}") if File.exists?("#{filename}.css") || File.exists?("#{filename}.css.scss") || File.exists?("#{filename}.css.sass") || File.exists?("#{filename}.scss") || File.exists?("#{filename}.sass")
20
+ out << javascript_include_tag("#{directory}#{filename}/#{filename}") if File.exists?("#{file_path}.js") || File.exists?("#{file_path}.js.coffee") || File.exists?("#{file_path}.coffee")
21
+ out << stylesheet_link_tag("#{directory}#{filename}/#{filename}") 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")
13
22
  end
14
23
 
15
24
  # Render the section partial into the view.
16
- filename = "#{Rails.root}/app/sections/#{name}/_#{name}.html"
17
- if File.exists?("#{filename}.erb") || File.exists?("#{filename}.haml")
18
- out << render(:partial => "/../sections/#{name}/#{name}")
25
+ partial_path = "#{directory_path}/_#{filename}.html"
26
+ if File.exists?("#{partial_path}.erb") || File.exists?("#{partial_path}.haml")
27
+ out << render(:partial => "/../sections/#{directory}#{filename}/#{filename}")
19
28
  else
20
- out << content_tag(:div, '', :class => name)
29
+ out << content_tag(:div, '', :class => filename)
21
30
  end
22
31
 
23
32
  out.join("\n").html_safe
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.1.1
4
+ version: 0.2.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-25 00:00:00.000000000 Z
12
+ date: 2012-05-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70096782295880 !ruby/object:Gem::Requirement
16
+ requirement: &70246963972680 !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: *70096782295880
24
+ version_requirements: *70246963972680
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70096782294280 !ruby/object:Gem::Requirement
27
+ requirement: &70246963971340 !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: *70096782294280
35
+ version_requirements: *70246963971340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70096782293220 !ruby/object:Gem::Requirement
38
+ requirement: &70246963970600 !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: *70096782293220
46
+ version_requirements: *70246963970600
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.