helmsman 0.0.1

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: 17ffb3e2f06409546207afd53827fa0cb5e58dc8
4
+ data.tar.gz: 70366137bfc394958f7cdfefa7c2ab6ee2af2fec
5
+ SHA512:
6
+ metadata.gz: 659c202d87b8e499766eb522c8aa48c2ba9b34ea6cad192c10b620904cf3a25be422c3113574fd4e1ac7626d71f8265616ccd2726390753fbe9e81f8bcdb7f2f
7
+ data.tar.gz: 15fa8c66a2d2d7a99cf1ffa86c42338f5b9019dcaae91886bae62e8a70ba9ff76ec0abcc8efb5775872e3cc9f5807906ea67dbde2e5910f4ccc81b7b35fe0597
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ helmsman
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - 2.0.0
5
+
6
+ script: "bundle exec rspec"
7
+
8
+ notifications:
9
+ disabled: false
10
+ recipients:
11
+ - xijo@gmx.de
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in helmsman.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # Helmsman
2
+
3
+ [![Build Status](https://secure.travis-ci.org/xijo/helmsman.png?branch=master)](https://travis-ci.org/xijo/helmsman) [![Gem Version](https://badge.fury.io/rb/helmsman.png)](http://badge.fury.io/rb/helmsman)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'helmsman'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ Helmsman adds the `helm` helper to your rails application views. Here is how you use it:
18
+
19
+ ```ruby
20
+ helm :pictures, url: 'http://defiant.ncc/pictures'
21
+ ```
22
+
23
+ The above call will produce the following html output
24
+
25
+ ```html
26
+ <li>
27
+ <a href='http://defiant.ncc/pictures'>Pictures</a>
28
+ </li>
29
+ ```
30
+
31
+ ### Building the link
32
+
33
+ There are multiple ways to build the link:
34
+
35
+ 1. provide the url parameter. The link will then be build by using the translation and that url.
36
+
37
+ ```ruby
38
+ helm :pictures, url: 'http://randompictures.com'
39
+ ```
40
+
41
+ 2. set helm values in a block.
42
+
43
+ ```ruby
44
+ helm :pictures do |entry|
45
+ entry.name = 'Some pictures'
46
+ entry.url = 'http://randompictures.com'
47
+ end
48
+ ```
49
+
50
+ 3. build everything manually
51
+
52
+ ```ruby
53
+ helm :pictures do |_|
54
+ link_to 'Some pictures', 'http://randompictures.com'
55
+ end
56
+ ```
57
+
58
+ ### Translation lookup
59
+
60
+ The first parameter will be used to lookup the translations.
61
+
62
+ e.g. `helm :edit` called from `app/views/pictures/_menu.html.*` uses the following translations
63
+
64
+ - en.pictures.menu.edit
65
+ - en.pictures.menu.edit_disabled
66
+
67
+ ### Highlight current
68
+
69
+ Helmsman will highlight the current entry by using the controller and action name.
70
+
71
+ Per default the first parameter will be treated as the controller name: `helm :pictures, url: pictures_url` highlights on every controller action of the pictures_controller.
72
+
73
+ You may customize the highlight options by providing a set of controller and/or action names. Here are some examples:
74
+
75
+ ```ruby
76
+ helm :bridge, controllers: :screens # on any screens controller action
77
+ helm :bridge, controllers: [:screens, :sensors] # on any screens and sensors controller action
78
+ helm :bridge, actions: :show # on bridges controller show action
79
+ helm :bridge, controllers: :screens, actions: [:show, :index] # only on screens controller #show and #index
80
+ ```
81
+
82
+ Anyway you are not forced to use that mechanism, you can also set `current` by hand:
83
+
84
+ ```ruby
85
+ helm :bridge, current: true
86
+ ```
87
+
88
+ ### Disabling and visibility
89
+
90
+ To set a helm disabled you may use the `disabled` option. It will then add a tooltip to that entry to explain why it was disabled.
91
+
92
+ ```ruby
93
+ admin_sidebar_entry :pictures, url: pictures_url, disabled: user_signed_in?
94
+ ```
95
+
96
+ If you want it to not be visible at all set the `visible` option to false.
97
+
98
+ ```ruby
99
+ admin_sidebar_entry :pictures, url: pictures_url, visible: user_signed_in?
100
+ ```
101
+
102
+ ### Additional content and nesting
103
+
104
+ As mentioned above you may additional content and nest helms into each other by using its yield feature. Here is a SLIM example:
105
+
106
+ ```slim
107
+ - helm :pictures, url: 'http://allpictures.com' do
108
+ ul
109
+ = helm :architecture
110
+ = helm :nature
111
+ ```
112
+
113
+ You can always use the current helm during that yield, so finding out whether it is disabled, current or anything is trivial:
114
+
115
+
116
+ ```ruby
117
+ admin_sidebar_entry :pictures, disabled: true, current: false do |entry|
118
+ puts entry.enabled? # false
119
+ puts entry.disabled? # true
120
+ puts entry.visible? # true
121
+ puts entry.current? # false
122
+ end
123
+ ```
124
+
125
+ ## Compatibility
126
+
127
+ Helmsman is working for rails 3 & 4 and for any ruby >= 1.9.3
128
+
129
+ ## Contributing
130
+
131
+ 1. Fork it
132
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
133
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
134
+ 4. Push to the branch (`git push origin my-new-feature`)
135
+ 5. Create new Pull Request
136
+
137
+ ## TODO
138
+
139
+ 1. configure helper name
140
+ 2. configure current/disabled class
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/helmsman.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'helmsman/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "helmsman"
8
+ spec.version = Helmsman::VERSION
9
+ spec.authors = ["Johannes Opper"]
10
+ spec.email = ["johannes.opper@gmail.com"]
11
+ spec.description = %q{Steers your navigation through the ocean of your application}
12
+ spec.summary = %q{Steers your navigation through the ocean of your application}
13
+ spec.homepage = "http://github.com/xijo/helmsman"
14
+ spec.license = "WTFPL"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'actionpack'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'simplecov'
27
+ end
@@ -0,0 +1,64 @@
1
+ module Helmsman
2
+ class Helm
3
+ attr_accessor :name, :url, :additional, :i18n_key
4
+ include ActionView::Helpers::TagHelper
5
+ include ActionView::Helpers::UrlHelper
6
+
7
+ def initialize(options = {})
8
+ @disabled = options.fetch(:disabled)
9
+ @visible = options.fetch(:visible)
10
+ @current = options.fetch(:current)
11
+ @i18n_key = options.fetch(:i18n_key)
12
+ @url = options[:url]
13
+ end
14
+
15
+ def to_s
16
+ visible? ? content_tag(:li, li_content, li_options) : ''.html_safe
17
+ end
18
+
19
+ def html_safe?
20
+ true
21
+ end
22
+
23
+ def to_str
24
+ to_s
25
+ end
26
+
27
+ def li_content
28
+ link = link_to_if(enabled?, name, url) if url
29
+ disabled? ? link : "#{link}#{additional}".html_safe
30
+ end
31
+
32
+ def li_options
33
+ if enabled?
34
+ { class: ('current-menu-item' if current?) }
35
+ else
36
+ { rel: 'tooltip', title: disabled_title, class: 'disabled-menu-item', data: { placement: 'bottom' } }
37
+ end
38
+ end
39
+
40
+ def name
41
+ I18n.translate(i18n_key)
42
+ end
43
+
44
+ def disabled_title
45
+ I18n.translate("#{i18n_key}_disabled")
46
+ end
47
+
48
+ def disabled?
49
+ @disabled
50
+ end
51
+
52
+ def visible?
53
+ @visible
54
+ end
55
+
56
+ def enabled?
57
+ !@disabled
58
+ end
59
+
60
+ def current?
61
+ @current
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails/railtie'
2
+
3
+ module Helmsman
4
+ class Railtie < ::Rails::Railtie
5
+ initializer 'helmsman.view_helper' do
6
+ ActionView::Base.send :include, Helmsman::ViewHelper
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Helmsman
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,36 @@
1
+ module Helmsman
2
+ module ViewHelper
3
+ # Returns true if one of the given controllers match the actual one.
4
+ # If actions are set they are taken into account.
5
+ def current_state_by_controller(*given_controller_names, actions: [])
6
+ if given_controller_names.any? { |name| name == controller_name.to_sym }
7
+ if actions.present?
8
+ Array(actions).include?(action_name.to_sym)
9
+ else
10
+ true
11
+ end
12
+ end
13
+ end
14
+
15
+ # Private part of actionpack/lib/action_view/helpers/translation_helper.rb
16
+ # Wrapped for clarification what that does.
17
+ def expand_i18n_key(key)
18
+ scope_key_by_partial(key)
19
+ end
20
+
21
+ def helm(key, options = {}, &block)
22
+ actions = options.fetch(:actions) { [] }
23
+ controllers = options.fetch(:controllers) { [key] }
24
+ disabled = options.fetch(:disabled) { false }
25
+ current = options.fetch(:current) { current_state_by_controller(*controllers, actions: actions) }
26
+ visible = options.fetch(:visible) { true }
27
+ url = options[:url]
28
+ i18n_key = expand_i18n_key(".#{key}")
29
+
30
+ entry = Helm.new(disabled: disabled, current: current, visible: visible, i18n_key: i18n_key, url: url)
31
+
32
+ entry.additional = capture(entry, &block) if block_given?
33
+ entry
34
+ end
35
+ end
36
+ end
data/lib/helmsman.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'i18n'
2
+ require 'active_support'
3
+ require 'action_view'
4
+
5
+ require 'helmsman/version'
6
+ require 'helmsman/helm'
7
+ require 'helmsman/view_helper'
8
+ require 'helmsman/railtie' if defined?(Rails)
9
+
10
+ module Helmsman
11
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ class FakeHelper
4
+ include Helmsman::ViewHelper
5
+ include ActionView::Context
6
+ include ActionView::Helpers::CaptureHelper
7
+ include ActionView::Helpers::TranslationHelper
8
+ include ActionView::Helpers::TagHelper
9
+ include ActionView::Helpers::UrlHelper
10
+
11
+ def controller_name
12
+ 'pictures'
13
+ end
14
+
15
+ def action_name
16
+ 'index'
17
+ end
18
+
19
+ def scope_key_by_partial(_)
20
+ 'i18n_path'
21
+ end
22
+ end
23
+
24
+ describe Helmsman::ViewHelper do
25
+ let(:helper) { FakeHelper.new }
26
+
27
+ describe '#helm' do
28
+ it 'highlights per class name on a given controller name' do
29
+ result = helper.helm(:pictures)
30
+ result.should be_current
31
+ end
32
+
33
+ it 'is possible to override the current state' do
34
+ result = helper.helm(:not_pictures, current: true)
35
+ result.should be_current
36
+ end
37
+
38
+ it 'highlights by action name as well' do
39
+ result = helper.helm(:pictures, actions: :show)
40
+ result.should_not be_current
41
+ result = helper.helm(:pictures, actions: :index)
42
+ result.should be_current
43
+ end
44
+
45
+ it 'passing disabled option disables the link' do
46
+ result = helper.helm(:pictures, disabled: true)
47
+ result.to_s.should_not include '<a href'
48
+ result.to_s.should include 'disabled-menu-item'
49
+ end
50
+
51
+ it 'adds block results to the result' do
52
+ result = helper.helm(:pictures) { |entry| 'jambalaia' }
53
+ result.to_s.should include 'jambalaia'
54
+ end
55
+
56
+ it 'generates a link if it got a url' do
57
+ result = helper.helm(:pictures, url: 'hellofoo.com')
58
+ result.to_s.should include '<a href="hellofoo.com">'
59
+ end
60
+
61
+ it 'generates html_safe output' do
62
+ result = helper.helm(:pictures)
63
+ result.to_s.should be_html_safe
64
+ end
65
+
66
+ it 'setting visible to false will output nothing' do
67
+ result = helper.helm(:pictures, url: 'http://defiant.ncc/pictures', visible: false)
68
+ result.to_s.should eq ''
69
+ end
70
+
71
+ describe 'yield the entry' do
72
+ it 'knows if its the current' do
73
+ helper.helm(:foo, current: true) { |e| e.should be_current }
74
+ end
75
+
76
+ it 'knows if it is disabled' do
77
+ helper.helm(:foo, disabled: true) do |entry|
78
+ entry.should be_disabled
79
+ entry.should_not be_enabled
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,13 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.adapters.define 'gem' do
4
+ add_filter '/spec/'
5
+ add_group 'Libraries', '/lib/'
6
+ end
7
+ SimpleCov.start 'gem'
8
+
9
+ require 'helmsman'
10
+
11
+ RSpec.configure do |config|
12
+ config.color_enabled = true
13
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: helmsman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Johannes Opper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '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: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Steers your navigation through the ocean of your application
84
+ email:
85
+ - johannes.opper@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .ruby-gemset
92
+ - .ruby-version
93
+ - .travis.yml
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - helmsman.gemspec
99
+ - lib/helmsman.rb
100
+ - lib/helmsman/helm.rb
101
+ - lib/helmsman/railtie.rb
102
+ - lib/helmsman/version.rb
103
+ - lib/helmsman/view_helper.rb
104
+ - spec/lib/helmsman/view_helper_spec.rb
105
+ - spec/spec_helper.rb
106
+ homepage: http://github.com/xijo/helmsman
107
+ licenses:
108
+ - WTFPL
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.0.3
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Steers your navigation through the ocean of your application
130
+ test_files:
131
+ - spec/lib/helmsman/view_helper_spec.rb
132
+ - spec/spec_helper.rb