ramenu 3.0.0

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.
@@ -0,0 +1,12 @@
1
+ module Ramenu
2
+
3
+ class Railtie < Rails::Railtie
4
+ initializer "ramenu.initialize" do
5
+ end
6
+ end
7
+
8
+ end
9
+
10
+ ActiveSupport.on_load(:action_controller) do
11
+ include Ramenu::ActionController
12
+ end
@@ -0,0 +1,23 @@
1
+ module Ramenu
2
+ # A menu definer to create menus in a block
3
+ class RamenuDefiner
4
+ attr_accessor :menus, :flags, :options
5
+
6
+ def initialize(menus, flags, options = {})
7
+ self.menus = menus
8
+ self.flags = flags
9
+ self.options = options
10
+ end
11
+
12
+ # create a new flag in a block
13
+ def set_flag(name, value = nil, options = {})
14
+ Ramenu.set_flag_in(flags, name, value, options)
15
+ end
16
+
17
+ # create a new menu in a block
18
+ def add_menu(name, path = nil, options = {}, &block)
19
+ options[:flag] = name if @options[:flag_for_menu] == true && name.is_a?(Symbol)
20
+ Ramenu.add_menu_to(menus, name, path, options, &block)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ module Ramenu
2
+ # flags
3
+ def self.static_flags(flagset = nil)
4
+ flagset = DEFAULT_GROUP if flagset.nil?
5
+ @ramenu_flags ||= {}
6
+ @ramenu_flags[flagset] ||= {}
7
+ end
8
+
9
+ # return static menus
10
+ def self.static_menus(menu = nil)
11
+ menu = DEFAULT_GROUP if menu.nil?
12
+ @ramenu_menus ||= {}
13
+ @ramenu_menus[menu] ||= []
14
+ end
15
+
16
+ # add a new flag to a set of flags
17
+ def self.set_flag_in(flags, name, value, options = {})
18
+ flags.merge!({ name => value }) unless name.nil? || value.nil?
19
+ end
20
+ # add a new menu element to a set of menus
21
+ def self.add_menu_to(menu, name, path, options = {}, &block)
22
+ menu << new_ramenu_element(name, path, options, &block)
23
+ end
24
+
25
+ # create a new menu element
26
+ def self.new_ramenu_element(name, path = nil, options = {}, &block)
27
+ elem = Menus::Element.new(name, path, options)
28
+ yield elem if block_given?
29
+ return elem
30
+ end
31
+
32
+ # menus
33
+ def self.add_menu(name, path, options = {}, &block)
34
+ menu = options[:menu]
35
+ add_menu_to(static_menus(menu), name, path, options, &block)
36
+ end
37
+
38
+ end
39
+
@@ -0,0 +1,14 @@
1
+ module Ramenu
2
+
3
+ module Version
4
+ MAJOR = 3
5
+ MINOR = 0
6
+ PATCH = 0
7
+ BUILD = nil
8
+
9
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
10
+ end
11
+
12
+ VERSION = Version::STRING
13
+
14
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ramenu/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Thomas Kienlen", "Simone Carletti"]
7
+ gem.email = "thomas.kienlen@lafourmi-immo.com"
8
+ gem.description = "Ramenu is a simple Ruby on Rails plugin for creating and managing navigation menus for a Rails project."
9
+ gem.summary = "Rails 'A la carte' menu plugin for creating and managing navigation menus."
10
+ gem.homepage = "https://github.com/lafourmi/ramenu"
11
+
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ gem.name = "ramenu"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Ramenu::VERSION
18
+
19
+ gem.add_dependency 'rails', ">= 3.0"
20
+ gem.add_development_dependency 'appraisal', ">= 0"
21
+ gem.add_development_dependency 'mocha', "~> 0.9.10"
22
+ gem.add_development_dependency 'yard', ">= 0"
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'rack-test'
25
+ unless ENV["CI"]
26
+ gem.add_development_dependency "turn", "~> 0.9" if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
27
+ end
28
+ end
@@ -0,0 +1,34 @@
1
+ Ramenu::Routes = ActionDispatch::Routing::RouteSet.new
2
+ Ramenu::Routes.draw do
3
+ match ':controller(/:action(/:id))'
4
+ end
5
+
6
+ ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
7
+ ActionController::Base.send :include, Ramenu::Routes.url_helpers
8
+
9
+ class ActiveSupport::TestCase
10
+
11
+ setup do
12
+ @routes = Ramenu::Routes
13
+ end
14
+
15
+
16
+ def controller
17
+ @controller_proxy ||= ControllerProxy.new(@controller)
18
+ end
19
+
20
+ class ControllerProxy
21
+ def initialize(controller)
22
+ @controller = controller
23
+ end
24
+ def method_missing(method, *args)
25
+ @controller.instance_eval do
26
+ m = method(method)
27
+ m.call(*args)
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+
@@ -0,0 +1,19 @@
1
+ require 'turn/autorun'
2
+ # http://www.mattsears.com/articles/2011/12/10/minitest-quick-reference
3
+
4
+ Turn.config do |c|
5
+ # use one of output formats:
6
+ # :outline - turn's original case/test outline mode [default]
7
+ # :progress - indicates progress with progress bar
8
+ # :dotted - test/unit's traditional dot-progress mode
9
+ # :pretty - new pretty reporter
10
+ # :marshal - dump output as YAML (normal run mode only)
11
+ # :cue - interactive testing
12
+ #c.format = :outline
13
+ c.format = :progress
14
+ # turn on invoke/execute tracing, enable full backtrace
15
+ c.trace = true
16
+ # use humanized test names (works only with :outline format)
17
+ c.natural = true
18
+ end
19
+
@@ -0,0 +1,22 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+
4
+ #require 'mocha'
5
+ #require 'dummy'
6
+
7
+ # minitest and turn
8
+ #require 'minitest_helper'
9
+
10
+ ENV["RAILS_ENV"] = "test"
11
+
12
+ require "active_support"
13
+ require "action_controller"
14
+ require "rails/railtie"
15
+ #require "i18n"
16
+
17
+ $:.unshift File.expand_path('../../lib', __FILE__)
18
+ require 'ramenu'
19
+
20
+ #ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
21
+
22
+
@@ -0,0 +1,109 @@
1
+ require 'test_helper'
2
+ require 'dummy'
3
+
4
+ Ramenu.configure do |config|
5
+ config.add_menu :static_menu, :root_path
6
+ config.add_menu :static_menu_bis, :root_path, :menu => :default
7
+
8
+ config.definer :user_menu, :flag_for_menu => true do |d|
9
+ d.add_menu :home, :root_path
10
+ d.add_menu :users, :users_path, :flag => :users do |menu|
11
+ menu.add_menu :dashboard, :dashboard_users_path
12
+ menu.add_menu :help, :help_users_path
13
+ end
14
+
15
+ d.set_flag :home, true
16
+ d.set_flag :users, false
17
+ end
18
+
19
+ end
20
+
21
+
22
+ class MenusTestController < ActionController::Base
23
+ add_menu :root, :extranet_root_path
24
+ add_menu :footab, :root_path
25
+ add_menu_for_current :current_page
26
+
27
+ set_flag :foo, true
28
+ set_flag :bar, false
29
+
30
+ definer :user_menu, :flag_for_menu => true do |d|
31
+ d.add_menu :dashboard, :root_path
32
+ d.add_menu :main_screen, :root_path
33
+
34
+ d.set_flag :dashboard, false
35
+ d.set_flag :dashboard_icon, :none
36
+ d.set_flag :main_screen_value, 2
37
+ end
38
+
39
+
40
+ def index
41
+ render :text => ''
42
+ end
43
+
44
+ def show
45
+ set_flag :bar, true
46
+ render :text => ''
47
+ end
48
+ end
49
+
50
+ class MenusTest < ActionController::TestCase
51
+ tests MenusTestController
52
+
53
+ def test_add_volatile_menu
54
+ get :index
55
+ assert_equal(:root, controller.volatile_menus(:default).first.name)
56
+ end
57
+
58
+ def test_add_menu_for_current
59
+ get :show
60
+ assert_equal('/menus_test/show', controller.volatile_menus(:default).last.path)
61
+ end
62
+
63
+ def test_add_static_menu
64
+ get :index
65
+ assert_equal(:static_menu, controller.menus(:default).first.name)
66
+ end
67
+
68
+ def test_static_and_volatile_menus
69
+ get :index
70
+ assert_equal(5, controller.menus(:default).count)
71
+ end
72
+
73
+ def test_set_volatile_flag
74
+ get :index
75
+ assert_equal(true, controller.flags(:default)[:foo])
76
+ end
77
+
78
+ def test_set_static_flag
79
+ get :index
80
+ assert_equal(true, controller.flags(:user_menu)[:home])
81
+ end
82
+
83
+ def test_static_and_volatile_flags
84
+ get :index
85
+ assert_equal(5, controller.flags(:user_menu).count)
86
+ end
87
+
88
+ def test_set_flag_within_method
89
+ get :show
90
+ assert_equal(true, controller.flags(:default)[:bar])
91
+ end
92
+
93
+ def test_add_menu_with_definer
94
+ get :index
95
+ assert_equal(:main_screen, controller.menus(:user_menu).last.name)
96
+ end
97
+
98
+ def test_definer_with_flag_for_menu_set
99
+ get :index
100
+ assert_equal(:main_screen, controller.menus(:user_menu).last.options[:flag])
101
+ end
102
+
103
+ def test_set_flag_with_definer
104
+ get :index
105
+ assert_equal(:none, controller.flags(:user_menu)[:dashboard_icon])
106
+ end
107
+
108
+ end
109
+
@@ -0,0 +1,117 @@
1
+ require 'test_helper'
2
+
3
+ class BuilderTest < ActionView::TestCase
4
+
5
+ def setup
6
+ @template = self
7
+ end
8
+
9
+
10
+ def test_initialize_should_require_context_and_elements
11
+ assert_raise(ArgumentError) { Ramenu::Menus::Builder.new }
12
+ assert_raise(ArgumentError) { Ramenu::Menus::Builder.new(@template) }
13
+ assert_nothing_raised { Ramenu::Menus::Builder.new(@template, []) }
14
+ end
15
+
16
+ def test_initialize_should_allow_options
17
+ assert_nothing_raised { Ramenu::Menus::Builder.new(@template, [], {}) }
18
+ end
19
+
20
+ def test_initialize_should_set_context
21
+ builder = Ramenu::Menus::Builder.new(@template, [])
22
+ assert_equal(@template, builder.instance_variable_get(:'@context'))
23
+ end
24
+
25
+ def test_initialize_should_set_elements
26
+ builder= Ramenu::Menus::Builder.new(@template, [1, 2])
27
+ assert_equal([1, 2], builder.instance_variable_get(:'@elements'))
28
+ end
29
+
30
+
31
+ def test_compute_name_with_symbol
32
+ builder = Ramenu::Menus::Builder.new(@template, [])
33
+ element = Ramenu::Menus::Element.new(:method_for_name, nil)
34
+
35
+ assert_equal("name from symbol", builder.send(:compute_name, element))
36
+ end
37
+
38
+ def test_compute_name_with_proc
39
+ builder = Ramenu::Menus::Builder.new(@template, [])
40
+ element = Ramenu::Menus::Element.new(proc { |template| template.send(:proc_for_name) }, nil)
41
+
42
+ assert_equal("name from proc", builder.send(:compute_name, element))
43
+ end
44
+
45
+ def test_compute_name_with_string
46
+ builder = Ramenu::Menus::Builder.new(@template, [])
47
+ element = Ramenu::Menus::Element.new("name from string", nil)
48
+
49
+ assert_equal("name from string", builder.send(:compute_name, element))
50
+ end
51
+
52
+
53
+ def test_compute_path_with_symbol
54
+ builder = Ramenu::Menus::Builder.new(@template, [])
55
+ element = Ramenu::Menus::Element.new(nil, :method_for_path)
56
+
57
+ assert_equal("http://localhost/#symbol", builder.send(:compute_path, element))
58
+ end
59
+
60
+ def test_compute_path_with_proc
61
+ builder = Ramenu::Menus::Builder.new(@template, [])
62
+ element = Ramenu::Menus::Element.new(nil, proc { |template| template.send(:proc_for_path) })
63
+
64
+ assert_equal("http://localhost/#proc", builder.send(:compute_path, element))
65
+ end
66
+
67
+ def test_compute_path_with_hash
68
+ builder = Ramenu::Menus::Builder.new(@template, [])
69
+ element = Ramenu::Menus::Element.new(nil, { :foo => "bar" })
70
+
71
+ assert_equal("http://localhost?foo=bar", builder.send(:compute_path, element))
72
+ end
73
+
74
+ def test_compute_path_with_string
75
+ builder = Ramenu::Menus::Builder.new(@template, [])
76
+ element = Ramenu::Menus::Element.new(nil, "http://localhost/#string")
77
+
78
+ assert_equal("http://localhost/#string", builder.send(:compute_path, element))
79
+ end
80
+
81
+ def test_empty_associated_flagset
82
+ builder = Ramenu::Menus::Builder.new(@template, [])
83
+ element = Ramenu::Menus::Element.new(nil, "http://localhost/")
84
+
85
+ assert_equal(nil, builder.send(:flag_for, element))
86
+ end
87
+
88
+ def test_associated_flagset
89
+ builder = Ramenu::Menus::Builder.new(@template, [], :flags => {:activated => true, :seen => 5})
90
+ element = Ramenu::Menus::Element.new(nil, "http://localhost/", :flag => :activated)
91
+
92
+ assert_equal(true, builder.send(:flag_for, element, :flag))
93
+ end
94
+
95
+ def url_for(params)
96
+ "http://localhost?" + params.to_param
97
+ end
98
+
99
+ protected
100
+
101
+ def method_for_name
102
+ "name from symbol"
103
+ end
104
+
105
+ def proc_for_name
106
+ "name from proc"
107
+ end
108
+
109
+ def method_for_path
110
+ "http://localhost/#symbol"
111
+ end
112
+
113
+ def proc_for_path
114
+ "http://localhost/#proc"
115
+ end
116
+
117
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class ElementTest < ActiveSupport::TestCase
4
+
5
+ def test_initialize_should_require_name_and_path
6
+ assert_raise(ArgumentError) { Ramenu::Menus::Element.new }
7
+ assert_raise(ArgumentError) { Ramenu::Menus::Element.new(nil) }
8
+ assert_nothing_raised { Ramenu::Menus::Element.new(nil, nil) }
9
+ end
10
+
11
+ def test_initialize_should_set_name
12
+ element = Ramenu::Menus::Element.new(:homepage, nil)
13
+ assert_equal :homepage, element.name
14
+ end
15
+
16
+ def test_initialize_should_set_path
17
+ element = Ramenu::Menus::Element.new(nil, "/")
18
+ assert_equal "/", element.path
19
+ end
20
+
21
+ def test_initialize_should_allow_options
22
+ element = Ramenu::Menus::Element.new(:homepage, "/", :title => "Go to the Homepage")
23
+ assert_equal({ :title => "Go to the Homepage" }, element.options)
24
+ end
25
+
26
+ def test_initialize_should_allow_childs
27
+ element = Ramenu::Menus::Element.new(:homepage, "/", :title => "Go to the Homepage")
28
+ element.add_child(nil, '/')
29
+ element.add_child(:homepage, nil)
30
+ assert_equal(2, element.childs.count)
31
+ end
32
+
33
+
34
+ def test_name
35
+ element = Ramenu::Menus::Element.new(nil, nil)
36
+ element.name = :the_name
37
+ assert_equal :the_name, element.name
38
+ end
39
+
40
+ def test_path
41
+ element = Ramenu::Menus::Element.new(nil, nil)
42
+ element.path = { :controller => "index", :action => "index" }
43
+ assert_equal({ :controller => "index", :action => "index" }, element.path)
44
+ end
45
+
46
+ def test_options
47
+ element = Ramenu::Menus::Element.new(nil, nil)
48
+ element.options = { :title => "Go to the Homepage" }
49
+ assert_equal({ :title => "Go to the Homepage" }, element.options)
50
+ end
51
+
52
+ end