trejo 0.0.1 → 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d7167dcc26e34868545129b750246edb2bd34df
4
+ data.tar.gz: aef5d381977f15648365cb1484031a10514b0735
5
+ SHA512:
6
+ metadata.gz: f4570e03d3106449ecdcd6d19e44e2ab99b82ea409ae7a86511a5ad3346246c3a0af4239815f0f64954f17ca82cbe85421bf28763e8fdbcff25fcb2aaeb91fc5
7
+ data.tar.gz: 3a6dbb02b6adbf83178b8162bdf2b0a09b1c0808a06052bc94671f6bb0fe5388529a67b837f386d6fe4f20f5773a38d6ef4092be38cb5712278e9445ed08c84f
data/README.md CHANGED
@@ -1,58 +1,184 @@
1
1
  # Trejo
2
2
 
3
- Trejo provides `nav_item` helper to render navigation links. An `active` class is applied to the link when the requested path matches the link url.
3
+ Trejo provides view helpers and utilities for common UI needs in Rails apps.
4
4
 
5
5
  ## Installation
6
6
 
7
- Add it to your Gemfile and run the `bundle` command to install it.
7
+ Add it to the Gemfile
8
8
 
9
- ```ruby
10
- gem 'trejo'
11
- ```
9
+ ```ruby
10
+ gem 'trejo'
11
+ ```
12
+
13
+ And run
14
+
15
+ ```
16
+ bundle
17
+ ```
18
+
19
+ Then run the installer to generate the initializer
20
+
21
+ ```
22
+ $ rails g trejo:install
23
+ ```
24
+
25
+ An initializer should now be at
26
+
27
+ ```
28
+ config/initializers/trejo.rb
29
+ ```
30
+
31
+ ### Configuration parameters
32
+
33
+ The initializer should look similar to the following
34
+
35
+ ```ruby
36
+ Trejo.configure do |config|
37
+ config.site_title = 'My Website Title'
38
+ config.company_name = 'My Company Name'
39
+ end
40
+ ```
41
+
42
+ Set the values for the config params `site_title` and/or `company_name` as needed.
12
43
 
13
44
  ## Usage
14
45
 
15
- If the current path is `/home`, then the following
46
+ ### nav_item
16
47
 
17
- ```
18
- <nav>
19
- <%= nav_item 'Home', '/home' %>
20
- <%= nav_item 'Blog', '/blog' %>
21
- </nav>
22
- ```
48
+ The `nav_item` helper renders a navigation link with `active` class when the requested path matches the link url.
23
49
 
24
- generates
50
+ For example, if the current path is `/home`, then the following
25
51
 
26
- ```
27
- <nav>
28
- <a href='/home' class='active'>Home</a>
29
- <a href='/blog'>Blog</a>
30
- </nav>
31
- ```
52
+ ```erb
53
+ <nav>
54
+ <%= nav_item 'Home', '/home' %>
55
+ <%= nav_item 'Blog', '/blog' %>
56
+ </nav>
57
+ ```
32
58
 
33
- Trejo assumes that the link url is the root of the resource, and ignores query parameters by default. So the above example also works if the requested path is `/home/index?foo=bar`.
59
+ renders
60
+
61
+ ```html
62
+ <nav>
63
+ <a href='/home' class='active'>Home</a>
64
+ <a href='/blog'>Blog</a>
65
+ </nav>
66
+ ```
67
+
68
+ `nav_item` assumes that the link url is the root of the resource, and ignores query parameters by default. So the above example also works if the requested path is `/home/index?foo=bar`.
34
69
 
35
70
  The default css class applied to the link is `active`. This can be overridden by passing a `class` option with the desired class.
36
71
 
37
- ```
38
- <%= nav_item 'Home', '/home', class: 'current-section' %>
39
- ```
72
+ ```erb
73
+ <%= nav_item 'Home', '/home', class: 'current-section' %>
74
+ ```
40
75
 
41
76
  generates
42
77
 
43
- ```
44
- <a href='/home' class='current-section'>Home</a>
45
- ```
78
+ ```html
79
+ <a href='/home' class='current-section'>Home</a>
80
+ ```
46
81
 
47
82
  If you need more granularity in the criteria for determining an active link, you can supply a regular expression in the `selected ` option. So if the current path is `/home?foo=bar`, then the following
48
83
 
49
- ```
50
- <%= nav_item 'Home', '/home?foo=bar', selected: /^\/home\?foo=\w+/ %>
51
- ```
84
+ ```erb
85
+ <%= nav_item 'Home', '/home?foo=bar', selected: /^\/home\?foo=\w+/ %>
86
+ ```
52
87
 
53
88
  generates
54
89
 
55
- ```
56
- <a class='active'>Home</a>
57
- ```
90
+ ```
91
+ <a class='active'>Home</a>
92
+ ```
93
+
94
+ ### merge_classes
95
+
96
+ The `merge_classes` helper takes any string or array or combination thereof to produce a string of css classes separated by a single whitespace.
97
+
98
+ ```ruby
99
+ merge_classes 'foo', 'bar' => 'foo bar'
100
+ merge_classes 'foo bar', 'baz' => 'foo bar baz'
101
+ merge_classes ['foo', 'bar'], 'baz' => 'foo bar baz'
102
+ merge_classes ['walter', 'sobchak'], ['shomer', ['shabbas']] => 'walter sobchak shomer shabbas'
103
+ ```
104
+
105
+ Whitespaces, duplicates and blank/nil values are omitted.
106
+
107
+ ### title
108
+
109
+ `title` generates the title tag for the current page.
110
+
111
+ In the application layout template, add the following
112
+
113
+ ```erb
114
+ <title><%= yield(:title).presence %></title>
115
+ ```
116
+
117
+ Then set the page title anywhere in the current page template
118
+
119
+ ```erb
120
+ <%= title 'About Us' %>
121
+ ```
122
+
123
+ which yields
124
+
125
+ ```html
126
+ <title>About Us | My Website Title</title>
127
+ ```
128
+
129
+ The website title defaults to the top level application module name. Set the config param `site_title` to customize this value.
130
+
131
+ ### copyright_notice
132
+
133
+ ```erb
134
+ <%= copyright_notice 'My Company Name' %>
135
+ ```
136
+
137
+ yields
138
+
139
+ ```html
140
+ © 2015 My Company Name, All Rights Reserved
141
+ ```
142
+
143
+ You can also call it without the company name
144
+
145
+ ```erb
146
+ <%= copyright_notice %>
147
+ ```
148
+
149
+ This defaults to the `company_name` config param, which itself defaults to the top level application module name.
150
+
151
+ In `config/initializers/trejo.rb`,
152
+
153
+ ```ruby
154
+ Trejo.configure do |config|
155
+ config.company_name = 'My Company Name'
156
+ end
157
+ ```
158
+
159
+ ### page_id, page_class
160
+
161
+ It's often useful to scope the current page's elements at the top level.
162
+
163
+ On the `body` tag, usually in the application layout, add the following
164
+
165
+ ```erb
166
+ <body id='<%= body_id %>' class='<%= body_class %>'>
167
+ </body>
168
+ ```
169
+
170
+ Then in the current template
171
+
172
+ ```erb
173
+ <%= page_id 'body-tag-id' %>
174
+ <%= page_class 'body-tag-class' %>
175
+ ```
176
+
177
+ If `page_class` is not called, then no class is applied.
178
+
179
+ If `page_id` is not called, then `body_id` outputs a dom id in the following format
180
+
181
+ ```ruby
182
+ "#{controller_name}-#{action_name}-page".dasherize
183
+ ```
58
184
 
@@ -0,0 +1,25 @@
1
+ Trejo.configure do |config|
2
+ # Setting site_title to 'My Website Title' and calling:
3
+ #
4
+ # title 'About Us'
5
+ #
6
+ # yields the title tag:
7
+ #
8
+ # <title>About Us | My Website Title</title>
9
+ #
10
+ # Defaults to the top level application module name
11
+ #
12
+ # config.site_title = 'My Website Title'
13
+
14
+ # Setting company_name to 'Church of Scientautology, LLC' and calling:
15
+ #
16
+ # copyright_notice
17
+ #
18
+ # yields the output text:
19
+ #
20
+ # © 2015 Church of Scientautology, LLC, All Rights Reserved
21
+ #
22
+ # Defaults to the top level application module name
23
+ #
24
+ # config.company_name = 'Church of Scientautology, LLC'
25
+ end
@@ -0,0 +1,12 @@
1
+ module Trejo
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../../templates', __FILE__)
5
+ desc 'Creates Trejo initializer for your application'
6
+
7
+ def copy_initializer
8
+ template 'initializer.rb', 'config/initializers/trejo.rb'
9
+ end
10
+ end
11
+ end
12
+ end
data/lib/trejo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Trejo
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -12,5 +12,40 @@ module Trejo
12
12
  link_class = options[:class] || 'active' if selected
13
13
  link_to name, url, class: link_class
14
14
  end
15
+
16
+ def merge_classes(*classes)
17
+ classes.flatten
18
+ .reject(&:blank?).map(&:split)
19
+ .flatten.uniq.join ' '
20
+ end
21
+
22
+ def title page_title
23
+ return if page_title.blank?
24
+ content_for(:title) { "#{page_title} | #{Trejo.configuration.site_title}" }
25
+ end
26
+
27
+ def body_id
28
+ if content_for?(:page_id)
29
+ content_for(:page_id)
30
+ else
31
+ "#{controller.controller_name}-#{controller.action_name}-page".dasherize
32
+ end
33
+ end
34
+
35
+ def body_class
36
+ content_for?(:page_class) ? content_for(:page_class) : nil
37
+ end
38
+
39
+ def page_id value
40
+ content_for(:page_id) { value }
41
+ end
42
+
43
+ def page_class value
44
+ content_for(:page_class) { value }
45
+ end
46
+
47
+ def copyright_notice company_name = nil
48
+ "\u00A9 #{Date.current.year} #{company_name.presence || Trejo.configuration.company_name}, All Rights Reserved"
49
+ end
15
50
  end
16
51
  end
data/lib/trejo.rb CHANGED
@@ -1,2 +1,22 @@
1
1
  require 'trejo/version'
2
2
  require 'trejo/railtie' if defined?(Rails)
3
+
4
+ module Trejo
5
+ class << self
6
+ attr_accessor :configuration
7
+ end
8
+
9
+ def self.configure
10
+ self.configuration ||= Configuration.new
11
+ yield configuration
12
+ end
13
+
14
+ class Configuration
15
+ attr_accessor :site_title, :company_name
16
+
17
+ def initialize
18
+ @site_title = Rails.application.class.parent_name
19
+ @company_name = Rails.application.class.parent_name
20
+ end
21
+ end
22
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'action_view'
2
+ require 'action_dispatch'
2
3
  require 'trejo/view_helpers'
@@ -1,134 +1,181 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  include ActionView::Helpers::UrlHelper
4
+ include ActionDispatch
4
5
  class TrejoHelperTestClass; end
5
6
 
6
7
  describe Trejo::ViewHelpers do
8
+ let(:trejo) do
9
+ TrejoHelperTestClass.new
10
+ end
11
+
7
12
  before do
8
- @trejo = TrejoHelperTestClass.new
9
- @trejo.extend Trejo::ViewHelpers
13
+ trejo.extend Trejo::ViewHelpers
10
14
  end
11
15
 
12
16
  describe '#nav_item' do
17
+ let(:request) do
18
+ instance_double ActionDispatch::Request, fullpath: fullpath
19
+ end
20
+
21
+ before do
22
+ expect(trejo).to receive(:request).and_return(request)
23
+ end
24
+
13
25
  context 'by default' do
14
- context 'when the link path matches the requested path' do
15
- before do
16
- request = stub fullpath: '/home'
17
- @trejo.stub(:request).and_return(request)
26
+ let(:fullpath) { '/home' }
18
27
 
19
- @nav_item = @trejo.nav_item('Home', '/home')
28
+ context 'when the link path matches the requested path' do
29
+ let(:nav_item) do
30
+ trejo.nav_item('Home', '/home')
20
31
  end
21
32
 
22
33
  it 'returns a selected nav item' do
23
- expect(@nav_item).to eq('<a href="/home" class="active">Home</a>')
34
+ expect(nav_item).to eq('<a class="active" href="/home">Home</a>')
24
35
  end
25
36
  end
26
37
 
27
38
  context 'when the link path does not match the requested path' do
28
- before do
29
- request = stub fullpath: '/home'
30
- @trejo.stub(:request).and_return(request)
31
-
32
- @nav_item = @trejo.nav_item('Blog', '/blog')
39
+ let(:nav_item) do
40
+ trejo.nav_item('Blog', '/blog')
33
41
  end
34
42
 
35
43
  it 'returns an unselected nav item' do
36
- expect(@nav_item).to eq('<a href="/blog">Blog</a>')
44
+ expect(nav_item).to eq('<a href="/blog">Blog</a>')
37
45
  end
38
46
  end
39
47
 
40
48
  context 'ignores query parameters' do
41
49
  context 'and when the link path matches the requested path' do
42
- before do
43
- request = stub fullpath: '/home?page=2'
44
- @trejo.stub(:request).and_return(request)
50
+ let(:fullpath) { '/home?page=2' }
45
51
 
46
- @nav_item = @trejo.nav_item('Home', '/home')
52
+ let(:nav_item) do
53
+ trejo.nav_item('Home', '/home')
47
54
  end
48
55
 
49
56
  it 'returns a selected nav item' do
50
- expect(@nav_item).to eq('<a href="/home" class="active">Home</a>')
57
+ expect(nav_item).to eq('<a class="active" href="/home">Home</a>')
51
58
  end
52
59
  end
53
60
  end
54
61
 
55
62
  context 'assumes the link path is the root' do
56
63
  context 'and when the requested path includes a nested path' do
57
- before do
58
- request = stub fullpath: '/home/index'
59
- @trejo.stub(:request).and_return(request)
60
- @nav_item = @trejo.nav_item('Home', '/home')
64
+ let(:fullpath) { '/home/index' }
65
+
66
+ let(:nav_item) do
67
+ trejo.nav_item('Home', '/home')
61
68
  end
62
69
 
63
70
  it 'returns a selected nav item' do
64
- expect(@nav_item).to eq('<a href="/home" class="active">Home</a>')
71
+ expect(nav_item).to eq('<a class="active" href="/home">Home</a>')
65
72
  end
66
73
  end
67
74
 
68
75
  context 'and when the requested path includes a different root path' do
69
- before do
70
- request = stub fullpath: '/blog/index'
71
- @trejo.stub(:request).and_return(request)
72
- @nav_item = @trejo.nav_item('Home', '/home')
76
+ let(:fullpath) { '/blog/index' }
77
+
78
+ let(:nav_item) do
79
+ trejo.nav_item('Home', '/home')
73
80
  end
74
81
 
75
82
  it 'returns an unselected nav item' do
76
- expect(@nav_item).to eq('<a href="/home">Home</a>')
83
+ expect(nav_item).to eq('<a href="/home">Home</a>')
77
84
  end
78
85
  end
79
86
 
80
87
  context 'and when query parameters are present' do
81
- before do
82
- request = stub fullpath: '/home/index?foo=bar&walter=sobchak'
83
- @trejo.stub(:request).and_return(request)
84
- @nav_item = @trejo.nav_item('Home', '/home')
88
+ let(:fullpath) { '/home/index?foo=bar&walter=sobchak' }
89
+
90
+ let(:nav_item) do
91
+ trejo.nav_item('Home', '/home')
85
92
  end
86
93
 
87
94
  it 'returns a selected nav item' do
88
- expect(@nav_item).to eq('<a href="/home" class="active">Home</a>')
95
+ expect(nav_item).to eq('<a class="active" href="/home">Home</a>')
89
96
  end
90
97
  end
91
98
  end
92
99
  end
93
100
 
94
101
  context 'when the selection criteria is a regex' do
95
- before do
96
- request = stub fullpath: '/home?page=2'
97
- @trejo.stub(:request).and_return(request)
98
- end
102
+ let(:fullpath) { '/home?page=2' }
99
103
 
100
104
  context 'when the requested path matches the criteria' do
101
- before do
102
- @nav_item = @trejo.nav_item('Home', '/home', selected: /^\/home/)
105
+ let(:nav_item) do
106
+ trejo.nav_item('Home', '/home', selected: /^\/home/)
103
107
  end
104
108
 
105
109
  it 'returns a selected nav item' do
106
- expect(@nav_item).to eq('<a href="/home" class="active">Home</a>')
110
+ expect(nav_item).to eq('<a class="active" href="/home">Home</a>')
107
111
  end
108
112
  end
109
113
 
110
114
  context 'when the requested path does not match the criteria' do
111
- before do
112
- @nav_item = @trejo.nav_item('Blog', '/blog', selected: /^\/blog/)
115
+ let(:nav_item) do
116
+ trejo.nav_item('Blog', '/blog', selected: /^\/blog/)
113
117
  end
114
118
 
115
119
  it 'returns a unselected nav item' do
116
- expect(@nav_item).to eq('<a href="/blog">Blog</a>')
120
+ expect(nav_item).to eq('<a href="/blog">Blog</a>')
117
121
  end
118
122
  end
119
123
  end
120
124
 
121
125
  context 'when a css class is supplied' do
122
- before do
123
- request = stub fullpath: '/home'
124
- @trejo.stub(:request).and_return(request)
126
+ let(:fullpath) { '/home' }
125
127
 
126
- @nav_item = @trejo.nav_item('Home', '/home', class: 'current-section')
128
+ let(:nav_item) do
129
+ trejo.nav_item('Home', '/home', class: 'current-section')
127
130
  end
128
131
 
129
132
  it 'returns a selected nav item with that class' do
130
- expect(@nav_item).to eq('<a href="/home" class="current-section">Home</a>')
133
+ expect(nav_item).to eq('<a class="current-section" href="/home">Home</a>')
131
134
  end
132
135
  end
133
136
  end
137
+
138
+ describe '#merge_classes' do
139
+ it 'merges strings with strings' do
140
+ expect(trejo.merge_classes('foo', 'bar')).to eq('foo bar')
141
+ expect(trejo.merge_classes('foo bar', 'baz')).to eq('foo bar baz')
142
+ end
143
+
144
+ it 'merges arrays with strings' do
145
+ expect(trejo.merge_classes(['foo', 'bar'], 'baz')).to eq('foo bar baz')
146
+ end
147
+
148
+ it 'merges arrays with arrays' do
149
+ expect(
150
+ trejo.merge_classes(['walter', 'sobchak'], ['shomer', ['shabbas']])
151
+ ).to eq('walter sobchak shomer shabbas')
152
+ end
153
+
154
+ it 'omits duplicates' do
155
+ expect(
156
+ trejo.merge_classes(
157
+ 'volta', 'cassandra gemini', 'volta', 'cygnus',
158
+ ['volta', 'vismund cygnus', 'cassandra', 'gemini']
159
+ )
160
+ ).to eq('volta cassandra gemini cygnus vismund')
161
+ end
162
+
163
+ it 'ignores blank values' do
164
+ expect(
165
+ trejo.merge_classes(
166
+ 'volta', 'cassandra gemini', nil,
167
+ ['', 'cygnus', ' ', 'vismund', nil]
168
+ )
169
+ ).to eq('volta cassandra gemini cygnus vismund')
170
+ end
171
+
172
+ it 'strips unnecessary whitespaces' do
173
+ expect(
174
+ trejo.merge_classes(
175
+ ' volta ', 'cassandra gemini ',
176
+ [' cygnus', ' ', ' vismund ']
177
+ )
178
+ ).to eq('volta cassandra gemini cygnus vismund')
179
+ end
180
+ end
134
181
  end
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trejo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Boram Yoon
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-18 00:00:00.000000000 Z
11
+ date: 2015-01-19 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: actionpack
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.0.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: Navigation links with active states based on current path
@@ -50,12 +45,14 @@ executables: []
50
45
  extensions: []
51
46
  extra_rdoc_files: []
52
47
  files:
53
- - .gitignore
54
- - .rspec
48
+ - ".gitignore"
49
+ - ".rspec"
55
50
  - CHANGELOG
56
51
  - Gemfile
57
52
  - README.md
58
53
  - Rakefile
54
+ - lib/generators/templates/initializer.rb
55
+ - lib/generators/trejo/install_generator.rb
59
56
  - lib/trejo.rb
60
57
  - lib/trejo/railtie.rb
61
58
  - lib/trejo/version.rb
@@ -65,27 +62,26 @@ files:
65
62
  - trejo.gemspec
66
63
  homepage: http://github.com/boram/trejo
67
64
  licenses: []
65
+ metadata: {}
68
66
  post_install_message:
69
67
  rdoc_options: []
70
68
  require_paths:
71
69
  - lib
72
70
  required_ruby_version: !ruby/object:Gem::Requirement
73
- none: false
74
71
  requirements:
75
- - - ! '>='
72
+ - - ">="
76
73
  - !ruby/object:Gem::Version
77
74
  version: 1.9.3
78
75
  required_rubygems_version: !ruby/object:Gem::Requirement
79
- none: false
80
76
  requirements:
81
- - - ! '>='
77
+ - - ">="
82
78
  - !ruby/object:Gem::Version
83
79
  version: '0'
84
80
  requirements: []
85
81
  rubyforge_project: trejo
86
- rubygems_version: 1.8.24
82
+ rubygems_version: 2.2.2
87
83
  signing_key:
88
- specification_version: 3
84
+ specification_version: 4
89
85
  summary: Navigation link helper
90
86
  test_files:
91
87
  - spec/spec_helper.rb