actionnav 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3139471d9e398daa9e8e509f374be2077ec99ee8
4
+ data.tar.gz: da0f5b8f07d8e02e2214261dfa6aafca1f870207
5
+ SHA512:
6
+ metadata.gz: a5f0b4e891687847b56360cf59f2d4300f42016336680d534061547fff5c85b4dafbeccdf207e671ad7892549da71d9d0b43c3b49a248cb6548feacff782dd10
7
+ data.tar.gz: e9bf9423862d3465a095485480742fffb6d394cfb1fe77ac53f496fe6fb1aabb5d378cccda5a9e2811ea74666e4e4c0e421719d75ab802cce871d36686f60bf0
checksums.yaml.gz.sig ADDED
Binary file
data.tar.gz.sig ADDED
Binary file
data/lib/action_nav.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'action_nav/base'
2
+
3
+ if defined?(Rails)
4
+ require 'action_nav/railtie'
5
+ end
@@ -0,0 +1,71 @@
1
+ require 'action_nav/item'
2
+ require 'action_nav/item_instance'
3
+ require 'action_nav/item_dsl'
4
+
5
+ module ActionNav
6
+ class Base
7
+
8
+ # Initialize a new navigation
9
+ #
10
+ # @param controller [ActionController::Base]
11
+ # @return [ActionNav::Base]
12
+ def initialize(controller)
13
+ @controller = controller
14
+ @active_paths = []
15
+ end
16
+
17
+ # The controller that initialized this navigation.
18
+ #
19
+ # @return [ActionController::Base]
20
+ attr_reader :controller
21
+ attr_reader :active_paths
22
+
23
+ # Return a full list of items for this instance as
24
+ # instances.
25
+ #
26
+ # @return [Array<ActionNav::ItemInstance>]
27
+ def items
28
+ @items ||= self.class.items.each_with_object([]) do |(key, item), array|
29
+ instance = ItemInstance.new(self, item)
30
+ unless instance.hidden?
31
+ array << instance
32
+ end
33
+ end
34
+ end
35
+
36
+ # Add an active navigation by passing the full path to
37
+ # active item.
38
+ def activate(*parts)
39
+ @active_paths.push(parts)
40
+ end
41
+ alias_method :active, :activate
42
+
43
+ # Is the given active path?
44
+ #
45
+ # @return [Boolean]
46
+ def active_path?(*parts)
47
+ @active_paths.any? do |path|
48
+ a = path.size.times.map { |i| path[0, path.size - i] }
49
+ a.include?(parts)
50
+ end
51
+ end
52
+
53
+ # Add a new item to this navigation
54
+ #
55
+ # @param id [Symbol]
56
+ # @return [ActionNav::Item]
57
+ def self.item(id, &block)
58
+ item = Item.new(nil, id)
59
+ item.dsl(&block) if block_given?
60
+ items[id] = item
61
+ end
62
+
63
+ # Return all items for this navigation
64
+ #
65
+ # @return [Hash]
66
+ def self.items
67
+ @items ||= {}
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,17 @@
1
+ require 'action_nav/set'
2
+
3
+ module ActionNav
4
+ module ControllerExtension
5
+
6
+ def self.included(base)
7
+ base.class_eval do
8
+ helper_method :navigation
9
+ end
10
+ end
11
+
12
+ def navigation
13
+ @navigation ||= Set.new(self)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,45 @@
1
+ require 'action_nav/item_dsl'
2
+
3
+ module ActionNav
4
+ class Item
5
+
6
+ def initialize(parent, id)
7
+ @id = id
8
+ @parent = parent
9
+ @children = {}
10
+ end
11
+
12
+ attr_reader :id
13
+ attr_reader :children
14
+ attr_accessor :title
15
+ attr_accessor :url
16
+ attr_accessor :description
17
+ attr_accessor :icon
18
+ attr_accessor :hide_unless
19
+
20
+ def path
21
+ @parent ? [@parent.path, id].flatten : [id]
22
+ end
23
+
24
+ def add_child(id, &block)
25
+ child_item = Item.new(self, id)
26
+ block.call(child_item) if block_given?
27
+ @children[id] = child_item
28
+ end
29
+
30
+ def child(*ids)
31
+ previous = self
32
+ ids.each do |id|
33
+ previous = previous.children[id]
34
+ return nil if previous.nil?
35
+ end
36
+ previous
37
+ end
38
+
39
+ def dsl(&block)
40
+ ItemDSL.new(self).instance_eval(&block)
41
+ self
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,35 @@
1
+ module ActionNav
2
+ class ItemDSL
3
+
4
+ def initialize(item)
5
+ @item = item
6
+ end
7
+
8
+ def title(title = nil, &block)
9
+ @item.title = block_given? ? block : title
10
+ end
11
+
12
+ def url(url = nil, &block)
13
+ @item.url = block_given? ? block : url
14
+ end
15
+
16
+ def description(description = nil, &block)
17
+ @item.description = block_given? ? block : description
18
+ end
19
+
20
+ def icon(icon = nil, &block)
21
+ @item.icon = block_given? ? block : icon
22
+ end
23
+
24
+ def hide_unless(&block)
25
+ @item.hide_unless = block
26
+ end
27
+
28
+ def item(id, &block)
29
+ child = @item.add_child(id)
30
+ child.dsl(&block)
31
+ child
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,90 @@
1
+ require 'active_support/inflector/methods'
2
+
3
+ module ActionNav
4
+ class ItemInstance
5
+
6
+ attr_reader :item
7
+
8
+ def initialize(base, item)
9
+ @base = base
10
+ @item = item
11
+ end
12
+
13
+ def id
14
+ @item.id
15
+ end
16
+
17
+ def path
18
+ @item.path
19
+ end
20
+
21
+ def items
22
+ @item.children.each_with_object([]) do |(_, item), array|
23
+ instance = ItemInstance.new(@base, item)
24
+ unless instance.hidden?
25
+ array << instance
26
+ end
27
+ end
28
+ end
29
+
30
+ def active?
31
+ @base.active_path?(*self.path)
32
+ end
33
+
34
+ def title
35
+ cache(:title) { parse(@item.title, ActiveSupport::Inflector.humanize(@item.id.to_s)) }
36
+ end
37
+
38
+ def description
39
+ cache(:description) { parse(@item.description) }
40
+ end
41
+
42
+ def description?
43
+ !!description
44
+ end
45
+
46
+ def url
47
+ cache(:url) { parse(@item.url, "/")}
48
+ end
49
+
50
+ def icon
51
+ cache(:icon) { parse(@item.icon) }
52
+ end
53
+
54
+ def icon?
55
+ !!icon
56
+ end
57
+
58
+ def hidden?
59
+ cache(:hidden?) do
60
+ if @item.hide_unless
61
+ parse(@item.hide_unless, false) == false
62
+ else
63
+ false
64
+ end
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def parse(item, default = nil)
71
+ if item.is_a?(Proc)
72
+ @base.controller.instance_eval(&item)
73
+ elsif item
74
+ item.to_s
75
+ else
76
+ default
77
+ end
78
+ end
79
+
80
+ def cache(name, &block)
81
+ @cache ||= {}
82
+ if @cache.keys.include?(name)
83
+ @cache[name]
84
+ else
85
+ @cache[name] = block.call
86
+ end
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,12 @@
1
+ module ActionNav
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer 'actionnav.initialize' do |app|
5
+ ActiveSupport.on_load :action_controller do
6
+ require 'action_nav/controller_extension'
7
+ include ActionNav::ControllerExtension
8
+ end
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_support/inflector/inflections'
2
+
3
+ module ActionNav
4
+ class Set
5
+
6
+ def initialize(controller)
7
+ @controller = controller
8
+ @navigations = {}
9
+ end
10
+
11
+ def [](name)
12
+ @navigations[name] ||= begin
13
+ "#{name.to_s}_navigation".classify.constantize.new(@controller)
14
+ end
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module ActionNav
2
+ VERSION = "1.0.0"
3
+ end
data/lib/actionnav.rb ADDED
@@ -0,0 +1 @@
1
+ require 'action_nav'
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: actionnav
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIEZDCCAsygAwIBAgIBATANBgkqhkiG9w0BAQsFADA8MQswCQYDVQQDDAJtZTEZ
14
+ MBcGCgmSJomT8ixkARkWCWFkYW1jb29rZTESMBAGCgmSJomT8ixkARkWAmlvMB4X
15
+ DTE4MDMwNTE3MzAwNVoXDTE5MDMwNTE3MzAwNVowPDELMAkGA1UEAwwCbWUxGTAX
16
+ BgoJkiaJk/IsZAEZFglhZGFtY29va2UxEjAQBgoJkiaJk/IsZAEZFg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icZJHv8lUj2Lc3bcirQk5eeOFRPVGQSpLLoqA7dtS7Jy4cv
33
+ 3c5m+HyxSxzlrcVHMAgJYemK0uhVQD9Y6JwHKDroWDH+MPALjlScw8ui1jmNuH31
34
+ n5JOH/07C4gYcwTjJmtoRSov46Z6Gn5cc6NFkQpA185pbRLqEDKzusXvBOQlAOLh
35
+ iyQrH6PJ0xgVJNYx+DLq3eFmo2hYJkw/lVhYAK+MdajtYJbD5VvCIEHO0d5RRgV+
36
+ qnCNZoPPy0UtRmGKZTMZvVJEZiw4g0fY
37
+ -----END CERTIFICATE-----
38
+ date: 2018-12-04 00:00:00.000000000 Z
39
+ dependencies:
40
+ - !ruby/object:Gem::Dependency
41
+ name: activesupport
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ description: A navigation manager for Rails applications.
55
+ email:
56
+ - me@adamcooke.io
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/action_nav.rb
62
+ - lib/action_nav/base.rb
63
+ - lib/action_nav/controller_extension.rb
64
+ - lib/action_nav/item.rb
65
+ - lib/action_nav/item_dsl.rb
66
+ - lib/action_nav/item_instance.rb
67
+ - lib/action_nav/railtie.rb
68
+ - lib/action_nav/set.rb
69
+ - lib/action_nav/version.rb
70
+ - lib/actionnav.rb
71
+ homepage: https://github.com/adamcooke/actionnav
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.5.2.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: This gem provides a friendly way to manage application navigation.
95
+ test_files: []
metadata.gz.sig ADDED
Binary file