activated_ui 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzAwMjE3ODFkN2M3ZmIwZmEzNjcxNjJjMTE4YjIxNDk1M2NhOTg5OQ==
5
+ data.tar.gz: !binary |-
6
+ NDA4OTdhZGVhODAxNWEwYWJlMmYxZGFlYTk1NTllZTBiNDFmMDM1Yg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ODk1MGQ2NzRjNjBjODVmNDU2MDQzMmQ4MzM2NzY5ODZjZTZkZDY4YzM5YjRk
10
+ OTM3ODhmNGQ2YzRlMWM0ZDRlNjYyZWIyMGNkODFkYzVlMThhM2FkNmNmYWI2
11
+ ODI2YTFmMTA5NDMxM2QxM2VmYTExMTBmZDBmN2M5ZWNmNzk0OGI=
12
+ data.tar.gz: !binary |-
13
+ NjJhNzVlZjE4OGI5YjE0YjFlMjhmMWMzNmIzYmQ1NWM2ZjM0NWI1YmQ2OGVi
14
+ M2M0ZmQwNThiY2U4M2UwOWYxMDJiYjIyNGEyNjczNGU5NTY4YmI1OWEwZWEy
15
+ NWVlMGUzZWY1ZTY5MzY4ZDc5ZWM5ODQ1NmFjN2Y4NGNmOGRmZDk=
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,87 @@
1
+ # ActivatedUI
2
+
3
+ ActivatedUI, is a collection of helpers to set and manage the active state of UI elements. It is similar to [active\_link\_to](https://github.com/twg/active_link_to) in that it is designed to help control the active state of UI elements for you. However where active\_link\_to infers the active state from the current url, ActivatedUI enables you to explicitly state it.
4
+
5
+
6
+ ## Installation
7
+
8
+ When installing for Rails 3 applications add this to the Gemfile: `gem 'activated_ui'` and run `bundle install`.
9
+
10
+ ActivatedUI _should_ work without rails, but I have to admit I haven't really used it in this environment.
11
+
12
+ ## Usage
13
+
14
+ ### Setting the Active State
15
+
16
+ You can define the active state with:
17
+
18
+ activate_ui_marked :my_key # active state is [:my_key]
19
+
20
+ You can give it as many keys at a time as you like:
21
+
22
+ activate_ui_marked :my_key, :another_key # active state is [:my_key, :another_key]
23
+
24
+ `activate_ui_marked` builds on itself like so:
25
+
26
+ activate_ui_marked :my_key # active state is [:my_key]
27
+ activate_ui_marked :another_key # active state is now [:my_key, :another_key]
28
+
29
+ ### Getting the Active State
30
+
31
+ Once you have set the active state there are a number of ways you can use it:
32
+
33
+ Find if a key is active:
34
+
35
+ ui_activated? :my_key # will return true if :my_key is in the active state
36
+ ui_activated :my_key # ui_activated? is aliased to ui_activated if you so prefer
37
+
38
+ Find if multiple keys are active:
39
+
40
+ ui_activated? :my_key, :another_key # will return true if all of the arguments are in the active state
41
+
42
+ Get a html active class value:
43
+
44
+ <li class='<%= activated_class :my_key %>'>My Item</li> <!-- will produce class='active' if :my_key is in the active state otherwise class='' -->
45
+
46
+ By default the returned class is 'active', but you can change this:
47
+
48
+ self.class.activated_class 'my_active_class'
49
+ activated_class :key # will now return 'my_active_class'
50
+
51
+ ## With Rails
52
+
53
+ ActivatedUI isn't dependant on Rails but if you are using it with Rails here's how:
54
+
55
+ class PostsController < ActionController::Base
56
+
57
+ include ActivatedUI
58
+
59
+ activate_ui_marked :posts # Adds :key to the active state using a call to before_filter
60
+ activate_ui_marked :posts, :only => [:show] # It will pass on any options, so you can use before_filter's :only and :except options
61
+
62
+ activated_class 'my_custom_active_class' # Change the return value of activated_class :key
63
+
64
+ def show
65
+ activate_ui_marked :show
66
+ # show.html.erb
67
+ end
68
+
69
+ end
70
+
71
+
72
+ Activated UI will add activated\_class, activate\_ui\_marked, stored\_activated_ui, ui\_activated? and ui\_activated as view helpers:
73
+
74
+ <% if ui_activated? :posts %>
75
+ <ul>
76
+ <li class='<%= activated_class :show %>'><%= activated_link_to :show, "Show Posts", post_path(@post), class: 'btn' %></li>
77
+ </ul>
78
+ <% end %>
79
+
80
+ It will also add a helper to wrap Rail's `link_to`:
81
+
82
+ <%= activated_link_to :show, "Show Posts", post_path(@post), class: 'btn' %>
83
+
84
+ It behaves the same way as `link_to` except there is an additional first argument that takes either a single key or an array of keys to check the active state with using `ui_activated?`. The active class is added to any classes you pass into via the options argument.
85
+
86
+
87
+
@@ -0,0 +1,61 @@
1
+ module ActivatedUI
2
+
3
+ def self.included(base)
4
+
5
+ # If the module is being included on a an Rails controller
6
+ if defined?(ActionController::Base) && (base < ActionController::Base)
7
+
8
+ base.helper_method :activated_class, :activate_ui_marked, :stored_activated_ui, :ui_activated, :ui_activated?
9
+
10
+ def base.activate_ui_marked *refs
11
+ params = refs.last.is_a?(Array) ? refs.pop : {}
12
+ before_filter params do
13
+ activate_ui_marked *refs
14
+ end
15
+ end
16
+
17
+ if defined?(ActionView::Base)
18
+ ActionView::Base.send :include, ActivatedUI::Helpers
19
+ end
20
+
21
+ end
22
+
23
+ def base.activated_class value=nil
24
+ @activated_class = value unless value.nil?
25
+ @activated_class || 'active'
26
+ end
27
+
28
+ end
29
+
30
+
31
+ def activated_class *refs
32
+ if ui_activated? *refs
33
+ self.class.activated_class
34
+ else
35
+ ""
36
+ end
37
+ end
38
+
39
+ def activate_ui_marked *refs
40
+ refs.each do |ref|
41
+ stored_activated_ui << ref
42
+ end
43
+ stored_activated_ui
44
+ end
45
+
46
+ def stored_activated_ui
47
+ @stored_activated_ui ||= []
48
+ end
49
+
50
+ def ui_activated? *refs
51
+ all_match = true
52
+ refs.each do |ref|
53
+ all_match &&= stored_activated_ui.include?(ref)
54
+ end
55
+ all_match
56
+ end
57
+ alias :ui_activated :ui_activated?
58
+
59
+ end
60
+
61
+ require 'activated_ui/helpers'
@@ -0,0 +1,13 @@
1
+ module ActivatedUI
2
+
3
+ module Helpers
4
+
5
+ def activated_link_to refs, name, url, link_options={}
6
+ refs = [refs] if refs.is_a? Symbol # If we're given a symbol make it an array
7
+ link_options[:class] = "#{link_options[:class]} #{activated_class(*refs)}".strip
8
+ link_to(name, url, link_options)
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,3 @@
1
+ module ActivatedUI
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+ require 'rails/all'
4
+
5
+ #uncomment the following line to use spork with the debugger
6
+ # require 'spork/ext/ruby-debug'
7
+
8
+ # @todo @dry this is repeated across the three projects
9
+ Spork.prefork do
10
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
11
+ ENV["RAILS_ENV"] ||= 'test'
12
+
13
+ require 'rspec/rails'
14
+ require 'rspec/autorun'
15
+
16
+ RSpec.configure do |config|
17
+
18
+ RESERVED_IVARS = %w(@loaded_fixtures)
19
+
20
+ # ## Mock Framework
21
+ #
22
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
23
+ #
24
+ # config.mock_with :mocha
25
+ # config.mock_with :flexmock
26
+ # config.mock_with :rr
27
+
28
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
29
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
30
+
31
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
32
+ # examples within a transaction, remove the following line or assign false
33
+ # instead of true.
34
+ # config.use_transactional_fixtures = true
35
+
36
+ # If true, the base class of anonymous controllers will be inferred
37
+ # automatically. This will be the default behavior in future versions of
38
+ # rspec-rails.
39
+ config.infer_base_class_for_anonymous_controllers = false
40
+
41
+ # Run specs in random order to surface order dependencies. If you find an
42
+ # order dependency and want to debug it, you can fix the order by providing
43
+ # the seed, which is printed after each run.
44
+ # --seed 1234
45
+ config.order = "random"
46
+
47
+ config.treat_symbols_as_metadata_keys_with_true_values = true
48
+
49
+ end
50
+
51
+ ActiveSupport::Dependencies.clear
52
+
53
+ end
54
+
55
+ Spork.each_run do
56
+
57
+ load File.dirname(__FILE__) + "/../lib/activated_ui.rb"
58
+
59
+ end
60
+
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActivatedUI do
4
+
5
+ include ActivatedUI
6
+
7
+ describe '#activate_ui_marked' do
8
+
9
+ it "adds a single passed symbol into stored_activated_ui" do
10
+ activate_ui_marked :a_symbol
11
+ stored_activated_ui.should eq [:a_symbol]
12
+ end
13
+
14
+ it "adds a multiple arguments" do
15
+ activate_ui_marked :a_symbol, :another_symbol, :and_another
16
+ stored_activated_ui.should eq [:a_symbol, :another_symbol, :and_another]
17
+ end
18
+
19
+ it "stacks values onto existing values" do
20
+ activate_ui_marked :a_symbol
21
+ activate_ui_marked :another_symbol
22
+ activate_ui_marked :and_another
23
+ stored_activated_ui.should eq [:a_symbol, :another_symbol, :and_another]
24
+ end
25
+
26
+ it "returns stored_activated_ui" do
27
+ returned_value = stored_activated_ui
28
+ activate_ui_marked(:something).should be returned_value
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActivatedUI do
4
+
5
+ include ActivatedUI
6
+
7
+ describe '#activated_class' do
8
+
9
+ context "when the ui is activated" do
10
+
11
+ before :each do
12
+ should_receive(:ui_activated?).with(:a_symbol).and_return(true)
13
+ end
14
+
15
+ it "return the configured active class" do
16
+ self.class.activated_class "my_active_class"
17
+ activated_class(:a_symbol).should eq "my_active_class"
18
+ end
19
+
20
+ it "returns 'active' if there isn't a configured class" do
21
+ self.class.activated_class false # Giving it false returns to default
22
+ activated_class(:a_symbol).should eq "active"
23
+ end
24
+
25
+ end
26
+
27
+ context "when the ui isn't activated" do
28
+
29
+ before :each do
30
+ should_receive(:ui_activated?).with(:not_the_symbol).and_return(false)
31
+ end
32
+
33
+ it "returns an empty string" do
34
+ activated_class(:not_the_symbol).should eq ""
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActivatedUI::Helpers do
4
+
5
+ include ActionView::Helpers::UrlHelper
6
+ include ActionView::Helpers::TagHelper
7
+ include ActivatedUI
8
+ include ActivatedUI::Helpers
9
+
10
+ describe '#activated_link_to' do
11
+
12
+ context "when the ui is activated" do
13
+
14
+ before :each do
15
+ should_receive(:ui_activated?).with(:a_symbol).and_return(true)
16
+ end
17
+
18
+ it "returns a link with a custom active class" do
19
+ self.class.activated_class "my_active_class"
20
+ activated_link_to(:a_symbol, "Link text", 'http://example.com').should eq '<a href="http://example.com" class="my_active_class">Link text</a>'
21
+ end
22
+
23
+ it "returns a link with 'active' class if there isn't a configured class" do
24
+ self.class.activated_class false # Giving it false returns to default
25
+ activated_link_to(:a_symbol, "Link text", 'http://example.com').should eq '<a href="http://example.com" class="active">Link text</a>'
26
+ end
27
+
28
+ it "gives additional arguments to link_to" do
29
+ link_text, link_url, link_options = "Link text", "http://example.com", { :class => "my_class" }
30
+ should_receive(:link_to).with(link_text, link_url, link_options)
31
+ activated_link_to(:a_symbol, link_text, link_url, link_options)
32
+ end
33
+
34
+ it "adds the active class to link_to's options" do
35
+ self.class.activated_class false # Giving it false returns to default
36
+ link_text, link_url, link_options = "Link text", "http://example.com", { :class => "my_class" }
37
+ should_receive(:link_to).with(link_text, link_url, { :class => "my_class active" })
38
+ activated_link_to(:a_symbol, link_text, link_url, link_options)
39
+ end
40
+
41
+ it "handles no provided class" do
42
+ self.class.activated_class false # Giving it false returns to default
43
+ link_text, link_url, link_options = "Link text", "http://example.com", { }
44
+ should_receive(:link_to).with(link_text, link_url, { :class => "active" })
45
+ activated_link_to(:a_symbol, link_text, link_url, link_options)
46
+ end
47
+
48
+ end
49
+
50
+ context "when the ui isn't activated" do
51
+
52
+ before :each do
53
+ should_receive(:ui_activated?).with(:not_the_symbol).and_return(false)
54
+ end
55
+
56
+ it "returns a link with no addition to the class" do
57
+ self.class.activated_class false # Giving it false returns to default
58
+ activated_link_to(:not_the_symbol, "Link text", 'http://example.com').should eq '<a href="http://example.com" class="">Link text</a>'
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ class MockActivatedUIController < ActionController::Base
4
+ include ActivatedUI
5
+ end
6
+
7
+ describe ActivatedUI do
8
+
9
+
10
+ describe 'self.activated_class' do
11
+
12
+ subject { MockActivatedUIController }
13
+
14
+ it "adds activated_class class method to set and get active class" do
15
+ subject.activated_class.should eq 'active'
16
+ subject.activated_class('my_active').should eq 'my_active'
17
+ subject.activated_class.should eq 'my_active'
18
+ subject.activated_class(false).should eq 'active'
19
+ subject.activated_class.should eq 'active'
20
+ end
21
+
22
+ end
23
+
24
+ describe 'helper methods' do
25
+
26
+ it "sets some of its methods as view helpers" do
27
+ class MockActivatedUIController2 < ActionController::Base ; end
28
+ MockActivatedUIController2.should_receive(:helper_method).with(:activated_class, :activate_ui_marked, :stored_activated_ui, :ui_activated, :ui_activated?)
29
+ class MockActivatedUIController2 < ActionController::Base
30
+ include ActivatedUI
31
+ end
32
+ end
33
+
34
+ it "adds ActivatedUI::Helpers to ActionView::Base" do
35
+ ActionView::Base.should_receive(:include).with(ActivatedUI::Helpers)
36
+ class MockActivatedUIController3 < ActionController::Base
37
+ include ActivatedUI
38
+ end
39
+ end
40
+
41
+ it "doesn't add ActivatedUI::Helpers to ActionView::Base if its not ActionController::Base" do
42
+ ActionView::Base.should_not_receive(:include).with(ActivatedUI::Helpers)
43
+ class MockActivatedUIController4
44
+ include ActivatedUI
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActivatedUI do
4
+
5
+ include ActivatedUI
6
+
7
+ describe '#stored_activated_ui' do
8
+
9
+ it "returns an empty array if @stored_activated_ui is not yet set" do
10
+ stored_activated_ui.should eq []
11
+ end
12
+
13
+ it "returns an @stored_activated_ui if its been set" do
14
+ returned_value = stored_activated_ui
15
+ stored_activated_ui.should be returned_value
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActivatedUI do
4
+
5
+ include ActivatedUI
6
+
7
+ describe '#ui_activated?' do
8
+
9
+ before :each do
10
+ should_receive(:stored_activated_ui).at_least(:once).and_return([:a, :b, :c])
11
+ end
12
+
13
+ it "returns true if the provided key is present in #stored_activated_ui" do
14
+ ui_activated?(:a).should be_true
15
+ end
16
+
17
+ it "returns false if the provided keys isn't present in #stored_activated_ui" do
18
+ ui_activated?(:d).should be_false
19
+ end
20
+
21
+ it "returns false if none of the provided keys are present in #stored_activated_ui" do
22
+ ui_activated?(:d, :e).should be_false
23
+ end
24
+
25
+ it "returns false if not all of the provided keys are present in #stored_activated_ui" do
26
+ ui_activated?(:a, :d).should be_false
27
+ end
28
+
29
+ it "returns true if the provided key is present in #stored_activated_ui" do
30
+ ui_activated?(:a).should be_true
31
+ end
32
+
33
+ it "returns true if all the provided key are present in #stored_activated_ui" do
34
+ ui_activated?(:a, :b).should be_true
35
+ end
36
+
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activated_ui
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Spence
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-28 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: 3.2.13
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.13
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: spork-rails
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: fuubar
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
+ description: Activated UI, is a collection of helpers to activate UI elements, it
70
+ achieves a similar end goal to active_link_to but you control the active state through
71
+ method calls rather than the helpers inferring it from the url.
72
+ email:
73
+ - msaspence@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - lib/activated_ui/helpers.rb
79
+ - lib/activated_ui/version.rb
80
+ - lib/activated_ui.rb
81
+ - MIT-LICENSE
82
+ - README.md
83
+ - spec/spec_helper.rb
84
+ - spec/unit/activated_ui/activate_ui_marked_spec.rb
85
+ - spec/unit/activated_ui/activated_class_spec.rb
86
+ - spec/unit/activated_ui/helpers/activated_link_to_spec.rb
87
+ - spec/unit/activated_ui/included_spec.rb
88
+ - spec/unit/activated_ui/stored_activated_ui_spec.rb
89
+ - spec/unit/activated_ui/ui_activated_spec.rb
90
+ homepage: http://github.com/msaspence/activated_ui
91
+ licenses: []
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Activated UI, is a collection of helpers to activate UI elements.
113
+ test_files:
114
+ - spec/spec_helper.rb
115
+ - spec/unit/activated_ui/activate_ui_marked_spec.rb
116
+ - spec/unit/activated_ui/activated_class_spec.rb
117
+ - spec/unit/activated_ui/helpers/activated_link_to_spec.rb
118
+ - spec/unit/activated_ui/included_spec.rb
119
+ - spec/unit/activated_ui/stored_activated_ui_spec.rb
120
+ - spec/unit/activated_ui/ui_activated_spec.rb