helmsman 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78f62fb01bec0e7c067b07f0fd15e8e1f526b4d6
4
- data.tar.gz: 8e0d19d3f3964254e6ccb51ce904e15863837016
3
+ metadata.gz: 0ca11064fdfec0f066477aa210a1a911cc1d9a6e
4
+ data.tar.gz: 6b6bd3981fc0454aac7028905abbfcadd2b23924
5
5
  SHA512:
6
- metadata.gz: 75931d6ccf84160e20579f09302af8d0a371d6cf9c4a472068bc77f87c02571a2d640fa10bd4f4f38dd75465c214cd1ce21078d8f5552847b0a7575c2a76024a
7
- data.tar.gz: 0f879d0ced6f48016929028e34824bf6a4a5484ae5c9e7144eb308a8f4356d5cf71d2236910a7f5a3d6f0960dcc7a2b28ac34ec5c6e20a5009e7c073785eb2c0
6
+ metadata.gz: 0e613a1a3378e25f4902a1040ad32366e1090273efc12dd83fc40d87a0b33588260389e67f64939765dbdbd084c3dc109083ac982421804b4ef6b29a40148096
7
+ data.tar.gz: 13b4562b71d09d2fe379d204dc319b95a3b1fe6ae97e9d1bf3e093982d9ae244bc5373a2bc27861debea0043251d357ba37638175a77ea54c87bb394c93e02df
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Helmsman
2
2
 
3
- [![Build Status](https://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) [![Code Climate](https://codeclimate.com/repos/523f6d34c7f3a34a75017842/badges/af7056196f756e73e540/gpa.png)](https://codeclimate.com/repos/523f6d34c7f3a34a75017842/feed)
3
+ [![Build Status](https://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
4
 
5
5
  ## Installation
6
6
 
@@ -70,13 +70,13 @@ Helmsman will highlight the current entry by using the controller and action nam
70
70
 
71
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
72
 
73
- You may customize the highlight options by providing a set of controller and/or action names. Here are some examples:
73
+ You may customize the highlight options by providing a set of controller and/or action names in the `highlight` options. Here are some examples:
74
74
 
75
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
76
+ helm :bridge, highlight: :screens # on any screens controller action
77
+ helm :bridge, highlight: [:screens, :sensors] # on any screens and sensors controller action
78
+ helm :bridge, highlight: { screens: :show } # on bridges controller show action
79
+ helm :bridge, highlight: :screens, sensors: [:show, :index] # only on screens controller #show and #index
80
80
  ```
81
81
 
82
82
  Anyway you are not forced to use that mechanism, you can also set `current` by hand:
@@ -1,3 +1,3 @@
1
1
  module Helmsman
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -12,6 +12,29 @@ module Helmsman
12
12
  end
13
13
  end
14
14
 
15
+ # Indicates if for the given conditions the helm should be highlighted.
16
+ # Examples:
17
+ # Given we are in pictures controller and action index
18
+ # highlight_helm?(:pictures) # true
19
+ # highlight_helm?(:foobar) # false
20
+ # highlight_helm?(pictures: :index) # true
21
+ # highlight_helm?(pictures: [:index, :show]) # true
22
+ # highlight_helm?(pictures: [:index, :show], :foobar) # true
23
+ def highlight_helm?(conditions)
24
+ Array(conditions).any? do |condition|
25
+ case condition
26
+ when Symbol, String
27
+ condition.to_s == controller_name
28
+ when Array
29
+ if condition.first.to_s == controller_name
30
+ Array(condition.last).any? { |given| given.to_s == action_name }
31
+ end
32
+ else
33
+ raise TypeError, "invalid format for highlight options, expected Symbol, Array or Hash got #{conditions.class.name}"
34
+ end
35
+ end
36
+ end
37
+
15
38
  # Private part of actionpack/lib/action_view/helpers/translation_helper.rb
16
39
  # Wrapped for clarification what that does.
17
40
  def expand_i18n_key(key)
@@ -19,13 +42,13 @@ module Helmsman
19
42
  end
20
43
 
21
44
  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}")
45
+ actions = options.fetch(:actions) { [] }
46
+ highlight_opts = options.fetch(:highlight) { key }
47
+ disabled = options.fetch(:disabled) { false }
48
+ current = options.fetch(:current) { highlight_helm?(highlight_opts) }
49
+ visible = options.fetch(:visible) { true }
50
+ url = options[:url]
51
+ i18n_key = expand_i18n_key(".#{key}")
29
52
 
30
53
  entry = Helm.new(disabled: disabled, current: current, visible: visible, i18n_key: i18n_key, url: url)
31
54
 
@@ -36,9 +36,9 @@ describe Helmsman::ViewHelper do
36
36
  end
37
37
 
38
38
  it 'highlights by action name as well' do
39
- result = helper.helm(:pictures, actions: :show)
39
+ result = helper.helm(:pictures, highlight: { pictures: :show })
40
40
  result.should_not be_current
41
- result = helper.helm(:pictures, actions: :index)
41
+ result = helper.helm(:pictures, highlight: { pictures: :index })
42
42
  result.should be_current
43
43
  end
44
44
 
@@ -80,5 +80,32 @@ describe Helmsman::ViewHelper do
80
80
  end
81
81
  end
82
82
  end
83
+
84
+ describe '#highlight_helm?' do
85
+ it 'highlights for a given controller' do
86
+ helper.highlight_helm?(:pictures).should be_true
87
+ helper.highlight_helm?(:romulans).should be_false
88
+ end
89
+
90
+ it 'accepts multiple controllers as highlight options' do
91
+ helper.highlight_helm?([:pictures, :romulans]).should be_true
92
+ helper.highlight_helm?([:borg, :romulans]).should be_false
93
+ end
94
+
95
+ it 'takes a flat conditions hash and treats it correctly' do
96
+ helper.highlight_helm?(pictures: :index).should be_true
97
+ helper.highlight_helm?(romulans: :attack).should be_false
98
+ end
99
+
100
+ it 'can highlight on multiple action names' do
101
+ helper.highlight_helm?(pictures: [:show, :index]).should be_true
102
+ end
103
+
104
+ it 'raises an type error for something unknown' do
105
+ expect {
106
+ helper.highlight_helm?(3)
107
+ }.to raise_error(TypeError)
108
+ end
109
+ end
83
110
  end
84
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helmsman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johannes Opper