Skip to content

Commit b5c4524

Browse files
committedApr 7, 2016
Update controllers_helper
* Update controllers_helper.md * Update controllers_helper.rb * Update controllers_helper_spec.rb
1 parent 3ba4422 commit b5c4524

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed
 

‎controllers_helper.md

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Rails controller and action helper for views
99

1010
## Usage
1111

12+
```ruby
13+
namespace?("admin")
14+
```
15+
1216
```ruby
1317
controller?("contents", "users")
1418
```

‎tools/controllers_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# encoding: UTF-8
22
module ControllersHelper
33

4+
def namespace?(*controller)
5+
controller.include?(params[:controller].split('/').first)
6+
end
7+
48
# TODO proof if this can be optimized with current_page?
59
# http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page-3F
610
def controller?(*controller)

‎tools/controllers_helper_spec.rb

+29-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,40 @@
33

44
describe ControllersHelper do
55

6+
describe "#namespace?" do
7+
before do
8+
allow(helper).to receive(:params).and_return(controller: "admin/contents")
9+
end
10+
11+
it "returns true if match" do
12+
expect(helper.namespace?("admin")).to be true
13+
end
14+
15+
it "returns true if match any" do
16+
expect(helper.namespace?("admin", "application")).to be true
17+
end
18+
19+
it "returns false if didn't match" do
20+
expect(helper.namespace?("application")).to be false
21+
end
22+
end
23+
624
describe "#controller?" do
725
before do
826
allow(helper).to receive(:params).and_return(controller: "contents")
927
end
1028

11-
it "returns true if contoller match" do
29+
it "returns true if match" do
1230
expect(helper.controller?("contents")).to be true
1331
end
1432

1533
it "returns true if match any" do
1634
expect(helper.controller?("contents", "users")).to be true
1735
end
36+
37+
it "returns false if didn't match any" do
38+
expect(helper.controller?("application", "users")).to be false
39+
end
1840
end
1941

2042

@@ -23,11 +45,15 @@
2345
allow(helper).to receive(:params).and_return(action: "index")
2446
end
2547

26-
it "returns true if action match" do
48+
it "returns true if match" do
2749
expect(helper.action?("index")).to be true
2850
end
2951

30-
it "returns false if action didn't match" do
52+
it "returns true if match any" do
53+
expect(helper.action?("index", "new")).to be true
54+
end
55+
56+
it "returns false if didn't match" do
3157
expect(helper.action?("new")).to be false
3258
end
3359
end

0 commit comments

Comments
 (0)
Please sign in to comment.