current 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Steven Garcia
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.
data/README.rdoc ADDED
@@ -0,0 +1,90 @@
1
+ = Current
2
+
3
+ Gives you a couple of simple helpers which check the current request in a Rails application.
4
+ It's sort of a current_page? on steroids
5
+
6
+ == Installation
7
+
8
+ gem install current
9
+
10
+ == Usage
11
+
12
+ Let's say you have a PostsController with a new action and you are currently on that page
13
+
14
+ controller_is('posts')
15
+ # => true
16
+
17
+ This method also accepts multiple arguments and will return true if any of them match
18
+
19
+ controller_is('posts','users')
20
+ # => true
21
+
22
+ controller_is('sessions','users')
23
+ # => false
24
+
25
+ The same logic applies to actions
26
+
27
+ action_is('new')
28
+ # => true
29
+
30
+ action_is('new','edit')
31
+ # => true
32
+
33
+ action_is('edit','index')
34
+ # => false
35
+
36
+ If you want to be specific:
37
+
38
+ controller_action_is('posts','new')
39
+ # => true
40
+
41
+ controller_action_is('posts','index')
42
+ # => false
43
+
44
+ Along with these methods you get a navigational link_to helper:
45
+
46
+ nav_link_to("Posts", posts_path, controller_is('posts'), options={})
47
+
48
+ # => "<a href="/posts" class="active"></a>"
49
+
50
+ nav_link_to("Users", users_path, controller_is('users'), options={})
51
+
52
+ # => "<a href="/posts" class="inactive"></a>"
53
+
54
+ Notice that the third argument is a condition which we conveniently slap our helper into
55
+ This will add a class of "active" or "inactive" to your link depending on whether that condition is true.
56
+
57
+ Note: An alternative for styling would be to include the controller and action names in your body id/class and use plain old CSS inheritance.
58
+
59
+ In your application layout
60
+
61
+ %body{:id => controller_name, :class => action_name}
62
+ %ul#navigation
63
+ %li= link_to "Posts", posts_path
64
+ %li= link_to "Users", users_path
65
+
66
+ In your stylesheet
67
+
68
+ ul#navigation li a {
69
+ background:white
70
+ }
71
+ body#posts ul#navigation li a,
72
+ body#users ul#navigation li a {
73
+ background:red
74
+ }
75
+
76
+ I prefer to explicitly use the "active" classes for cleaner CSS, but whatever floats your boat.
77
+
78
+ == Note on Patches/Pull Requests
79
+
80
+ * Fork the project.
81
+ * Make your feature addition or bug fix.
82
+ * Add tests for it. This is important so I don't break it in a
83
+ future version unintentionally.
84
+ * Commit, do not mess with rakefile, version, or history.
85
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
86
+ * Send me a pull request. Bonus points for topic branches.
87
+
88
+ == Copyright
89
+
90
+ Copyright (c) 2010 Steven Garcia. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "current"
8
+ gem.summary = "Series of convenience to test which page you are on"
9
+ gem.description = "Picks up where Rails current_page? helper leaves off"
10
+ gem.email = "stevendgarcia@gmail.com"
11
+ gem.homepage = "http://github.com/activestylus/current"
12
+ gem.authors = ["Steven Garcia"]
13
+ gem.version = "1.0.4"
14
+ gem.add_development_dependency "shoulda", ">= 0"
15
+ gem.add_development_dependency "mocha", ">= 0"
16
+ gem.add_development_dependency "rails", ">= 0"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/test_*.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "current #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/current.gemspec ADDED
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{current}
8
+ s.version = "1.0.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Steven Garcia"]
12
+ s.date = %q{2010-10-31}
13
+ s.description = %q{Picks up where Rails current_page? helper leaves off}
14
+ s.email = %q{stevendgarcia@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "current.gemspec",
26
+ "init.rb",
27
+ "lib/current.rb",
28
+ "test/helper.rb",
29
+ "test/test_current.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/activestylus/current}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.7}
35
+ s.summary = %q{Series of convenience to test which page you are on}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_current.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
47
+ s.add_development_dependency(%q<mocha>, [">= 0"])
48
+ s.add_development_dependency(%q<rails>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<shoulda>, [">= 0"])
51
+ s.add_dependency(%q<mocha>, [">= 0"])
52
+ s.add_dependency(%q<rails>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<shoulda>, [">= 0"])
56
+ s.add_dependency(%q<mocha>, [">= 0"])
57
+ s.add_dependency(%q<rails>, [">= 0"])
58
+ end
59
+ end
60
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'current'
data/lib/current.rb ADDED
@@ -0,0 +1,37 @@
1
+ module Current
2
+
3
+ def controller_is(*attrs)
4
+ attrs.collect{|attr| attr.to_s}.include?(controller_name)
5
+ alias_method :controller_is?, :controller_is
6
+ end
7
+
8
+ def action_is(*attrs)
9
+ attrs.map{|attr| attr.to_s}.include?(action_name)
10
+ alias_method :action_is?, :action_is
11
+ end
12
+
13
+ def partial_is(param)
14
+ param == params[:partial]
15
+ alias_method :partial_is?, :partial_is
16
+ end
17
+
18
+ def controller_action_is(c,a)
19
+ controller_is(c) && action_is(a)
20
+ alias_method :controller_action_is?, :controller_action_is
21
+ end
22
+
23
+ def active_if(condition)
24
+ condition ? "active" : "inactive"
25
+ alias_method :active_if?, :active_if
26
+ end
27
+
28
+ def nav_link_to(text,path,condition, options={})
29
+ klass = active_if(condition) + " #{options[:class]}"
30
+ linktext = condition ? "#{text}" : text
31
+ link_to(raw(linktext), path, options.merge(:class => klass.strip))
32
+ end
33
+
34
+ end
35
+
36
+
37
+ ActionView::Base.send :include, Current
data/test/helper.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'action_controller'
3
+ require 'shoulda'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+ require 'current'
@@ -0,0 +1,89 @@
1
+ require 'helper'
2
+
3
+ class UsersController < ActionController::Base
4
+ def new
5
+ end
6
+ def edit
7
+ end
8
+ end
9
+
10
+ class JobsController < ActionController::Base
11
+ def new
12
+ end
13
+ end
14
+
15
+ class TestCurrent < ActionView::TestCase
16
+
17
+ include Current
18
+
19
+ context "When on the users controller" do
20
+ setup do
21
+ @controller = UsersController.new
22
+ self.stubs(:controller).returns(@controller)
23
+ @request = ActionController::TestRequest.new
24
+ @request.stubs(:host).returns('localhost:3000')
25
+ @controller.stubs(:request).returns(@request)
26
+ @response = ActionController::TestResponse.new
27
+ self.stubs(:controller_name).returns('users')
28
+ self.stubs(:action_name).returns('new')
29
+ end
30
+
31
+ context "controller_is" do
32
+ should "be true for users" do
33
+ assert controller_is('users')
34
+ end
35
+
36
+ should "be true for users/new" do
37
+ assert controller_is('users', 'new')
38
+ end
39
+
40
+ should "be false for jobs" do
41
+ assert !controller_is('jobs')
42
+ end
43
+ end
44
+
45
+ context "action_is" do
46
+ should "be true if current action is in the supplied list" do
47
+ assert action_is('new')
48
+ assert action_is('new', 'edit')
49
+ end
50
+
51
+ should "be false if current action is not in the supplied list" do
52
+ assert !action_is('edit')
53
+ end
54
+ end
55
+
56
+ context "partial_is" do
57
+ should "be true if params[:partial] is the specified value" do
58
+ self.stubs(:params).returns({ :partial => 'boards' })
59
+ assert partial_is('boards')
60
+ end
61
+
62
+ should "be false if params[:partial] is not the specified value" do
63
+ self.stubs(:params).returns({ :partial => 'askjfafew' })
64
+ assert !partial_is('boards')
65
+ end
66
+ end
67
+
68
+ context "controller_action_is" do
69
+ should "be true if for current controller/action combination" do
70
+ assert controller_action_is('users', 'new')
71
+ end
72
+
73
+ should "be false for incorrect controller/action combination" do
74
+ assert !controller_action_is('accounts', 'edit')
75
+ assert !controller_action_is('jobs', 'new')
76
+ end
77
+ end
78
+
79
+ context "active_if" do
80
+ should "return active if true" do
81
+ assert_equal 'active', active_if(true)
82
+ end
83
+
84
+ should "return inactive if false" do
85
+ assert_equal 'inactive', active_if(false)
86
+ end
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: current
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 4
10
+ version: 1.0.4
11
+ platform: ruby
12
+ authors:
13
+ - Steven Garcia
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-31 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mocha
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rails
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ description: Picks up where Rails current_page? helper leaves off
64
+ email: stevendgarcia@gmail.com
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files:
70
+ - LICENSE
71
+ - README.rdoc
72
+ files:
73
+ - .document
74
+ - .gitignore
75
+ - LICENSE
76
+ - README.rdoc
77
+ - Rakefile
78
+ - current.gemspec
79
+ - init.rb
80
+ - lib/current.rb
81
+ - test/helper.rb
82
+ - test/test_current.rb
83
+ has_rdoc: true
84
+ homepage: http://github.com/activestylus/current
85
+ licenses: []
86
+
87
+ post_install_message:
88
+ rdoc_options:
89
+ - --charset=UTF-8
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.3.7
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Series of convenience to test which page you are on
117
+ test_files:
118
+ - test/helper.rb
119
+ - test/test_current.rb