simple-navigation 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ *master
2
+
3
+ * renderers now have access to request_forgery_protection stuff (this allows delete-links as navigation-items)
4
+
1
5
  *1.2.1
2
6
 
3
7
  * changed way to include render_*-helper_methods into view (including them into Controller and declaring them as helper_methods instead of adding whole module as Helper). this seems to be more reliable under certain conditions. Credits to Gernot Kogler.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
3
  :minor: 2
4
- :patch: 1
4
+ :patch: 2
@@ -3,6 +3,7 @@ module SimpleNavigation
3
3
 
4
4
  mattr_accessor :config_file
5
5
  mattr_accessor :config_file_path
6
+ mattr_accessor :controller
6
7
 
7
8
  # Reads the specified config_file and stores it for later evaluation.
8
9
  def self.load_config
@@ -10,15 +10,29 @@ module SimpleNavigation
10
10
  attr_accessor :selected_class
11
11
  attr_reader :primary_navigation
12
12
 
13
- # Evals the config_file inside the specified context (usually a controller or view)
14
- def self.eval_config(context)
15
- context.instance_eval(SimpleNavigation.config_file)
16
- end
13
+ class << self
17
14
 
18
- # Starts processing the configuration
19
- def self.run(&block)
20
- block.call Configuration.instance
21
- end
15
+ # Evals the config_file inside the specified context (usually a controller or view)
16
+ def eval_config(context)
17
+ context.instance_eval(SimpleNavigation.config_file)
18
+ SimpleNavigation.controller = extract_controller_from context
19
+ end
20
+
21
+ # Starts processing the configuration
22
+ def run(&block)
23
+ block.call Configuration.instance
24
+ end
25
+
26
+ # Extracts a controller from the context.
27
+ def extract_controller_from(context)
28
+ if context.respond_to? :controller
29
+ context.controller
30
+ else
31
+ context
32
+ end
33
+ end
34
+
35
+ end
22
36
 
23
37
  # Sets the config's default-settings
24
38
  def initialize
@@ -36,6 +50,7 @@ module SimpleNavigation
36
50
  def loaded?
37
51
  !@primary_navigation.nil?
38
52
  end
53
+
39
54
  end
40
55
 
41
56
  end
@@ -2,14 +2,15 @@ module SimpleNavigation
2
2
 
3
3
  # Represents an item in your navigation. Gets generated by the item method in the config-file.
4
4
  class Item
5
- attr_reader :key, :name, :url, :sub_navigation
5
+ attr_reader :key, :name, :url, :sub_navigation, :method
6
6
 
7
7
  # see ItemContainer#item
8
- def initialize(key, name, url, html_options, sub_nav_block)
8
+ def initialize(key, name, url, options, sub_nav_block)
9
9
  @key = key
10
+ @method = options.delete(:method)
10
11
  @name = name
11
12
  @url = url
12
- @html_options = html_options
13
+ @html_options = options
13
14
  if sub_nav_block
14
15
  @sub_navigation = ItemContainer.new
15
16
  sub_nav_block.call @sub_navigation
@@ -10,13 +10,27 @@ module SimpleNavigation
10
10
  include ActionView::Helpers::UrlHelper
11
11
  include ActionView::Helpers::TagHelper
12
12
 
13
- attr_reader :current_navigation, :current_sub_navigation
13
+ attr_reader :current_navigation, :current_sub_navigation, :controller
14
+
15
+ class << self
16
+
17
+ # Delegates method calls to the controller.
18
+ def controller_method(*methods)
19
+ methods.each do |method|
20
+ delegate method, :to => :controller
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ controller_method :form_authenticity_token, :protect_against_forgery?, :request_forgery_protection_token
14
27
 
15
28
  def initialize(current_navigation, current_sub_navigation=nil) #:nodoc:
16
29
  @current_navigation = current_navigation
17
30
  @current_sub_navigation = current_sub_navigation
31
+ @controller = SimpleNavigation.controller
18
32
  end
19
-
33
+
20
34
  # Renders the specified ItemContainer to HTML.
21
35
  #
22
36
  # If <tt>include_sub_navigation</tt> is set to true, the renderer should nest the sub_navigation for the active navigation
@@ -24,7 +38,7 @@ module SimpleNavigation
24
38
  def render(item_container, include_sub_navigation=false)
25
39
  raise 'subclass responsibility'
26
40
  end
27
-
41
+
28
42
  end
29
43
  end
30
44
  end
@@ -12,7 +12,7 @@ module SimpleNavigation
12
12
  def render(item_container, include_sub_navigation=false)
13
13
  list_content = item_container.items.inject([]) do |list, item|
14
14
  html_options = item.html_options(current_navigation)
15
- li_content = link_to(item.name, item.url, :class => item.selected_class(current_navigation))
15
+ li_content = link_to(item.name, item.url, :class => item.selected_class(current_navigation), :method => item.method)
16
16
  li_content << (item.sub_navigation.render(current_sub_navigation)) if include_sub_navigation && item.sub_navigation && item.selected?(current_navigation)
17
17
  list << content_tag(:li, li_content, html_options)
18
18
  end
@@ -17,6 +17,7 @@ describe SimpleNavigation::Configuration do
17
17
  describe 'self.eval_config' do
18
18
  before(:each) do
19
19
  @context = mock(:context)
20
+ @context.stub!(:instance_eval)
20
21
  @config_file = stub(:config_file)
21
22
  SimpleNavigation.stub!(:config_file).and_return(@config_file)
22
23
  end
@@ -24,8 +25,38 @@ describe SimpleNavigation::Configuration do
24
25
  @context.should_receive(:instance_eval).with(@config_file)
25
26
  SimpleNavigation::Configuration.eval_config(@context)
26
27
  end
28
+ it "should set the controller" do
29
+ @controller = stub(:controller)
30
+ SimpleNavigation::Configuration.should_receive(:extract_controller_from).with(@context).and_return(@controller)
31
+ SimpleNavigation.should_receive(:controller=).with(@controller)
32
+ SimpleNavigation::Configuration.eval_config(@context)
33
+ end
27
34
  end
28
35
 
36
+ describe 'self.extract_controller_from' do
37
+ before(:each) do
38
+ @nav_context = stub(:nav_context)
39
+ end
40
+
41
+ context 'object responds to controller' do
42
+ before(:each) do
43
+ @controller = stub(:controller)
44
+ @nav_context.stub!(:controller).and_return(@controller)
45
+ end
46
+
47
+ it "should return the controller" do
48
+ SimpleNavigation::Configuration.extract_controller_from(@nav_context).should == @controller
49
+ end
50
+
51
+ end
52
+
53
+ context 'object does not respond to controller' do
54
+ it "should return the nav_context" do
55
+ SimpleNavigation::Configuration.extract_controller_from(@nav_context).should == @nav_context
56
+ end
57
+ end
58
+ end
59
+
29
60
  describe 'initialize' do
30
61
  it "should set the List-Renderer as default upon initialize" do
31
62
  @config.renderer.should == SimpleNavigation::Renderer::List
@@ -21,6 +21,7 @@ describe SimpleNavigation::ItemContainer do
21
21
 
22
22
  before(:each) do
23
23
  @item_container.stub!(:should_add_item?).and_return(true)
24
+ @options = {}
24
25
  end
25
26
 
26
27
  context 'block given' do
@@ -30,28 +31,28 @@ describe SimpleNavigation::ItemContainer do
30
31
  end
31
32
 
32
33
  it "should should yield an new ItemContainer" do
33
- @item_container.item('key', 'name', 'url', 'options') do |container|
34
+ @item_container.item('key', 'name', 'url', @options) do |container|
34
35
  container.should == @sub_container
35
36
  end
36
37
  end
37
38
  it "should create a new Navigation-Item with the given params and the specified block" do
38
- SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', 'options', @proc)
39
- @item_container.item('key', 'name', 'url', 'options', &@proc)
39
+ SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', @options, @proc)
40
+ @item_container.item('key', 'name', 'url', @options, &@proc)
40
41
  end
41
42
  it "should add the created item to the list of items" do
42
43
  @item_container.items.should_receive(:<<)
43
- @item_container.item('key', 'name', 'url', 'options') {}
44
+ @item_container.item('key', 'name', 'url', @options) {}
44
45
  end
45
46
  end
46
47
 
47
48
  context 'no block given' do
48
49
  it "should create a new Navigation_item with the given params and nil as sub_navi" do
49
- SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', 'options', nil)
50
- @item_container.item('key', 'name', 'url', 'options')
50
+ SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', @options, nil)
51
+ @item_container.item('key', 'name', 'url', @options)
51
52
  end
52
53
  it "should add the created item to the list of items" do
53
54
  @item_container.items.should_receive(:<<)
54
- @item_container.item('key', 'name', 'url', 'options')
55
+ @item_container.item('key', 'name', 'url', @options)
55
56
  end
56
57
  end
57
58
 
@@ -3,7 +3,29 @@ require File.dirname(__FILE__) + '/../../spec_helper'
3
3
  describe SimpleNavigation::Item do
4
4
 
5
5
  describe 'initialize' do
6
-
6
+ context 'method' do
7
+ context 'defined' do
8
+ before(:each) do
9
+ @options = {:method => :delete}
10
+ @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', @options, nil)
11
+ end
12
+ it 'should set the method as instance_var' do
13
+ @item.method.should == :delete
14
+ end
15
+ it 'should set the html-options without the method' do
16
+ @item.instance_variable_get(:@html_options).key?(:method).should be_false
17
+ end
18
+ end
19
+
20
+ context 'undefined' do
21
+ before(:each) do
22
+ @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
23
+ end
24
+ it 'should set the instance-var to nil' do
25
+ @item.method.should be_nil
26
+ end
27
+ end
28
+ end
7
29
  end
8
30
 
9
31
  describe 'selected?' do
@@ -94,7 +116,7 @@ describe SimpleNavigation::Item do
94
116
  it {@item.html_options(:bla)[:id].should == 'my_key'}
95
117
  end
96
118
  end
97
-
119
+
98
120
  end
99
121
 
100
122
 
@@ -2,6 +2,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper'
2
2
 
3
3
  describe SimpleNavigation::Renderer::Base do
4
4
  before(:each) do
5
+ @controller = stub(:controller)
6
+ SimpleNavigation.stub!(:controller).and_return(@controller)
5
7
  @base_renderer = SimpleNavigation::Renderer::Base.new(:current_primary, :current_secondary)
6
8
  end
7
9
  it "should inclue ActionView::Helpers::UrlHelper" do
@@ -10,6 +12,45 @@ describe SimpleNavigation::Renderer::Base do
10
12
  it "should include ActionView::Helpers::TagHelper" do
11
13
  @base_renderer.should respond_to(:content_tag)
12
14
  end
13
- it {@base_renderer.current_navigation.should == :current_primary}
14
- it {@base_renderer.current_sub_navigation.should == :current_secondary}
15
+
16
+ describe 'delegated methods' do
17
+ it {@base_renderer.should respond_to(:form_authenticity_token)}
18
+ it {@base_renderer.should respond_to(:protect_against_forgery?)}
19
+ it {@base_renderer.should respond_to(:request_forgery_protection_token)}
20
+ end
21
+
22
+ describe 'initialize' do
23
+ it {@base_renderer.current_navigation.should == :current_primary}
24
+ it {@base_renderer.current_sub_navigation.should == :current_secondary}
25
+ it {@base_renderer.controller.should == @controller}
26
+ end
27
+
28
+ describe 'controller_method' do
29
+ context 'delegate a single method' do
30
+ before(:each) do
31
+ @base_renderer.class_eval do
32
+ controller_method :my_method
33
+ end
34
+ end
35
+ it 'should delegate a controller_method to the controller' do
36
+ @controller.should_receive(:my_method)
37
+ @base_renderer.my_method
38
+ end
39
+ end
40
+
41
+ context 'delegate multiple methods' do
42
+ before(:each) do
43
+ @base_renderer.class_eval do
44
+ controller_method :test1, :test2
45
+ end
46
+ end
47
+ it 'should delegate all controller_methods to the controller' do
48
+ @controller.should_receive(:test1)
49
+ @base_renderer.test1
50
+ @controller.should_receive(:test2)
51
+ @base_renderer.test2
52
+ end
53
+ end
54
+ end
55
+
15
56
  end
@@ -1,6 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe SimpleNavigation do
4
+
4
5
  describe 'load_config' do
5
6
  context 'config_file_path is set' do
6
7
  before(:each) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-navigation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andi Schacke