live-front-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5dc31d13b1ac2ac3790da8c8a36fb9bedc294081
4
+ data.tar.gz: a45aa4c6f7589f15b5b498718cb755f747b5c9a2
5
+ SHA512:
6
+ metadata.gz: 58e1c519b556b4cbb8b15a0834d98835d1bbba0a528c07371186aa66e5b7b8897eacdd5d40186cefd8b448568fd8f99d078f8c7c6cf3e777168be78dbed2c386
7
+ data.tar.gz: a922a58c81f7cc1c490f28c40ddfeba7bcfd4e7dac4a1459df86fd18db36819fb0832df860c0c11e57b547fa0a675c9525a9db381ebe5489fa12df6097f161c9
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in live-front-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andrey Krivko
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Live Front
2
+
3
+ Collection of useful helpers and workflows used at [Live Typing](http://ltst.ru).
4
+
5
+ ## List of helpers
6
+
7
+ ### TabHelper
8
+
9
+ Navigation link helper. Returns an `li` element with an 'active' class if the supplied
10
+ controller(s) and/or action(s) are currently active. The content of the
11
+ element is the value passed to the block. Initially was copied from
12
+ [GitLab project](https://gitlab.com/gitlab-org/gitlab-ce). Also it was added ability to specify
13
+ `params` option to make link active when current path suits specified params.
14
+
15
+ **options**
16
+
17
+ The options hash used to determine if the element is "active" (default: {})
18
+
19
+ `:controller` - One or more controller names to check (optional).
20
+
21
+ `:action` - One or more action names to check (optional).
22
+
23
+ `:path` - A shorthand path, such as 'dashboard#index', to check (optional).
24
+
25
+ `:params` - One or more params to check. It also can be specified `:_blank` value to be able to specify either blank or non-blank param (optional).
26
+
27
+ `:html_options` - Extra options to be passed to the list element (optional).
28
+
29
+ **block**
30
+
31
+ An optional block that will become the contents of the returned `li` element.
32
+
33
+ When both `:controller` and `:action` are specified, BOTH must match in order
34
+ to be marked as active. When only one is given, either can match.
35
+
36
+ #### Examples:
37
+
38
+ Assuming we're on `TreeController#show`
39
+
40
+ ```
41
+ # Controller matches, but action doesn't
42
+ nav_link(controller: [:tree, :refs], action: :edit) { "Hello" }
43
+ # => '<li>Hello</li>'
44
+ ```
45
+
46
+ ```
47
+ # Controller matches
48
+ nav_link(controller: [:tree, :refs]) { "Hello" }
49
+ # => '<li class="active">Hello</li>'
50
+ ```
51
+
52
+ ```
53
+ # Shorthand path
54
+ nav_link(path: 'tree#show') { "Hello" }
55
+ # => '<li class="active">Hello</li>'
56
+ ```
57
+
58
+ ```
59
+ # Supplying custom options for the list element
60
+ nav_link(controller: :tree, html_options: {class: 'home'}) { "Hello" }
61
+ # => '<li class="home active">Hello</li>'
62
+ ```
63
+
64
+ ```
65
+ # Supplying params options when `params[:state] # => 'archived'`
66
+ nav_link(path: 'tree#show', params: {state: 'archived'}) { "Hello" }
67
+ # => '<li class="home active">Hello</li>'
68
+ ```
69
+
70
+ ```
71
+ # Supplying params options when `params[:state] # => nil`
72
+ nav_link(path: 'tree#show', params: {state: 'archived'}) { "Hello" }
73
+ # => '<li class="home">Hello</li>'
74
+ ```
75
+
76
+ ```
77
+ # Supplying params options when `params[:state] # => nil`
78
+ nav_link(path: 'tree#show', params: {state: [:_blank, :active]}) { "Hello" }
79
+ # => '<li class="home active">Hello</li>'
80
+ ```
81
+
82
+ ## Installation
83
+
84
+ Add this line to your application's Gemfile:
85
+
86
+ ```ruby
87
+ gem 'live-front-rails', github: 'LiveTyping/live-front-rails'
88
+ ```
89
+
90
+ And then execute:
91
+
92
+ $ bundle
93
+
94
+ Or install it yourself as:
95
+
96
+ $ gem install live-front-rails
97
+
98
+ ## Contributing
99
+
100
+ 1. Fork it ( https://github.com/LiveTyping/live-front-rails/fork )
101
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
102
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
103
+ 4. Push to the branch (`git push origin my-new-feature`)
104
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color']
8
+ end
9
+
10
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ require 'live-front/version'
2
+ require 'live-front/application_helper'
3
+ require 'live-front/tab_helper'
4
+
5
+ module LiveFront
6
+ if defined?(Rails)
7
+ class Railtie < ::Rails::Railtie
8
+ initializer 'live_front.view_helpers' do
9
+ ActionView::Base.send :include, LiveFront::ApplicationHelper
10
+ ActionView::Base.send :include, LiveFront::TabHelper
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,31 @@
1
+ module LiveFront
2
+ module ApplicationHelper
3
+ # Check if a particular controller is the current one
4
+ #
5
+ # args - One or more controller names to check
6
+ #
7
+ # Examples
8
+ #
9
+ # # On TreeController
10
+ # current_controller?(:tree) # => true
11
+ # current_controller?(:commits) # => false
12
+ # current_controller?(:commits, :tree) # => true
13
+ def current_controller?(*args)
14
+ args.any? { |v| v.to_s.downcase == controller.controller_name }
15
+ end
16
+
17
+ # Check if a particular action is the current one
18
+ #
19
+ # args - One or more action names to check
20
+ #
21
+ # Examples
22
+ #
23
+ # # On Projects#new
24
+ # current_action?(:new) # => true
25
+ # current_action?(:create) # => false
26
+ # current_action?(:new, :create) # => true
27
+ def current_action?(*args)
28
+ args.any? { |v| v.to_s.downcase == action_name }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,102 @@
1
+ module LiveFront
2
+ # Copied from GitLab project
3
+ # https://gitlab.com/gitlab-org/gitlab-ce
4
+ module TabHelper
5
+ # Navigation link helper
6
+ #
7
+ # Returns an `li` element with an 'active' class if the supplied
8
+ # controller(s) and/or action(s) are currently active. The content of the
9
+ # element is the value passed to the block.
10
+ #
11
+ # options - The options hash used to determine if the element is "active" (default: {})
12
+ # :controller - One or more controller names to check (optional).
13
+ # :action - One or more action names to check (optional).
14
+ # :path - A shorthand path, such as 'dashboard#index', to check (optional).
15
+ # :html_options - Extra options to be passed to the list element (optional).
16
+ # block - An optional block that will become the contents of the returned
17
+ # `li` element.
18
+ #
19
+ # When both :controller and :action are specified, BOTH must match in order
20
+ # to be marked as active. When only one is given, either can match.
21
+ #
22
+ # Examples
23
+ #
24
+ # # Assuming we're on TreeController#show
25
+ #
26
+ # # Controller matches, but action doesn't
27
+ # nav_link(controller: [:tree, :refs], action: :edit) { "Hello" }
28
+ # # => '<li>Hello</li>'
29
+ #
30
+ # # Controller matches
31
+ # nav_link(controller: [:tree, :refs]) { "Hello" }
32
+ # # => '<li class="active">Hello</li>'
33
+ #
34
+ # # Shorthand path
35
+ # nav_link(path: 'tree#show') { "Hello" }
36
+ # # => '<li class="active">Hello</li>'
37
+ #
38
+ # # Supplying custom options for the list element
39
+ # nav_link(controller: :tree, html_options: {class: 'home'}) { "Hello" }
40
+ # # => '<li class="home active">Hello</li>'
41
+ #
42
+ # Returns a list item element String
43
+ def nav_link(options = {}, &block)
44
+ c, a = fetch_controller_and_action(options)
45
+ p = options.delete(:params) || {}
46
+
47
+ klass = page_active?(c, a, p) ? 'active' : ''
48
+
49
+
50
+ # Add our custom class into the html_options, which may or may not exist
51
+ # and which may or may not already have a :class key
52
+ o = options.delete(:html_options) || {}
53
+ o[:class] = "#{o[:class]} #{klass}".strip
54
+
55
+ if block_given?
56
+ content_tag(:li, capture(&block), o)
57
+ else
58
+ content_tag(:li, nil, o)
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ def fetch_controller_and_action(options)
65
+ path = options.delete(:path)
66
+
67
+ if path
68
+ if path.respond_to?(:each)
69
+ c = path.map { |p| p.split('#').first }
70
+ a = path.map { |p| p.split('#').last }
71
+ else
72
+ c, a, _ = path.split('#')
73
+ end
74
+ else
75
+ c = options.delete(:controller)
76
+ a = options.delete(:action)
77
+ end
78
+ [c, a]
79
+ end
80
+
81
+ def page_active?(controller, action, parameters = {})
82
+ match_action_controller = if controller && action
83
+ # When given both options, make sure BOTH are active
84
+ current_controller?(*controller) && current_action?(*action)
85
+ else
86
+ # Otherwise check EITHER option
87
+ current_controller?(*controller) || current_action?(*action)
88
+ end
89
+
90
+ match_action_controller && match_params?(parameters)
91
+ end
92
+
93
+ def match_params?(parameters)
94
+ return true if parameters.empty?
95
+
96
+ parameters.all? do |key, value|
97
+ value = *value
98
+ value.any? { |v| v == :_blank ? params[key].to_s.blank? : v.to_s == params[key].to_s }
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module LiveFront
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ require File.expand_path('../lib/live-front/version', __FILE__)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'live-front-rails'
6
+ spec.version = LiveFront::VERSION
7
+ spec.authors = ['Andrey Krivko']
8
+ spec.email = ['jastkand@gmail.com']
9
+ spec.summary = 'Useful helpers used at Live Typing'
10
+ spec.description = 'Useful helpers used at Live Typing'
11
+ spec.homepage = 'https://github.com/LiveTyping/live-front-rails'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files`.split("\n")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'rails', '~> 4.1'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.1.0'
24
+ spec.add_development_dependency 'rspec-mocks', '~> 3.1.0'
25
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe LiveFront::TabHelper do
4
+ describe '#nav_link' do
5
+ before do
6
+ allow(self).to receive(:action_name).and_return('foo')
7
+ end
8
+
9
+ it 'captures block output' do
10
+ expect(nav_link { 'Testing Blocks' }).to match(/Testing Blocks/)
11
+ end
12
+
13
+ it 'performs checks on the current controller' do
14
+ expect(nav_link(controller: :foo)).to match(/<li class="active">/)
15
+ expect(nav_link(controller: :bar)).not_to match(/active/)
16
+ expect(nav_link(controller: [:foo, :bar])).to match(/active/)
17
+ end
18
+
19
+ it 'performs checks on the current action' do
20
+ expect(nav_link(action: :foo)).to match(/<li class="active">/)
21
+ expect(nav_link(action: :bar)).not_to match(/active/)
22
+ expect(nav_link(action: [:foo, :bar])).to match(/active/)
23
+ end
24
+
25
+ it 'performs checks on both controller and action when both are present' do
26
+ expect(nav_link(controller: :bar, action: :foo)).not_to match(/active/)
27
+ expect(nav_link(controller: :foo, action: :bar)).not_to match(/active/)
28
+ expect(nav_link(controller: :foo, action: :foo)).to match(/active/)
29
+ end
30
+
31
+ it 'accepts a path shorthand' do
32
+ expect(nav_link(path: 'foo#bar')).not_to match(/active/)
33
+ expect(nav_link(path: 'foo#foo')).to match(/active/)
34
+ end
35
+
36
+ it 'accepts a params value and checks if they are correct' do
37
+ expect(nav_link(path: 'foo#foo', params: { p1: :s1 })).to match(/active/)
38
+ expect(nav_link(path: 'foo#foo', params: { p2: [:_blank, :s2] })).to match(/active/)
39
+ expect(nav_link(path: 'foo#foo', params: { p2: :s1 })).not_to match(/active/)
40
+ end
41
+
42
+ it 'passes extra html options to the list element' do
43
+ expect(
44
+ nav_link(action: :foo, html_options: { class: 'home' })
45
+ ).to match(/<li class="home active">/)
46
+ expect(nav_link(html_options: { class: 'active' })).to match(/<li class="active">/)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,28 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+
3
+ require 'active_support'
4
+ require 'action_view'
5
+ require 'action_controller'
6
+
7
+ require 'live-front-rails'
8
+
9
+ include ActionView::Context
10
+ include ActionView::Helpers::FormTagHelper
11
+ include LiveFront::ApplicationHelper
12
+ include LiveFront::TabHelper
13
+
14
+ class FooController < ActionController::Base
15
+ def foo
16
+ end
17
+ end
18
+
19
+ def controller
20
+ FooController.new
21
+ end
22
+
23
+ def params
24
+ ActionController::Parameters.new p1: 's1'
25
+ end
26
+
27
+ require 'rspec/core'
28
+ require 'rspec/mocks'
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: live-front-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Krivko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-mocks
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.0
83
+ description: Useful helpers used at Live Typing
84
+ email:
85
+ - jastkand@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/live-front-rails.rb
96
+ - lib/live-front/application_helper.rb
97
+ - lib/live-front/tab_helper.rb
98
+ - lib/live-front/version.rb
99
+ - live-front-rails.gemspec
100
+ - spec/helpers/tab_helper_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: https://github.com/LiveTyping/live-front-rails
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.5
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Useful helpers used at Live Typing
126
+ test_files:
127
+ - spec/helpers/tab_helper_spec.rb
128
+ - spec/spec_helper.rb