Class: ActiveScaffold::DataStructures::ActionLinks
- Includes:
- Enumerable
- Defined in:
- lib/active_scaffold/data_structures/action_links.rb
Constant Summary collapse
- COLLECTION_CLICK_MENU_LINK =
member so it’s cached
ActionLink.new(:index, position: false, type: :member, toggle: false, parameters: {action_links: '--ACTION-LINKS--', id: nil})
- MEMBER_CLICK_MENU_LINK =
ActionLink.new(:index, position: false, type: :member, toggle: false, parameters: {action_links: '--ACTION-LINKS--'})
Instance Attribute Summary collapse
-
#click_menu ⇒ Object
writeonly
Sets the attribute click_menu.
-
#css_class ⇒ Object
Returns the value of attribute css_class.
-
#default_type ⇒ Object
Returns the value of attribute default_type.
- #label(record) ⇒ Object
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
(also: #name_to_cache)
readonly
Returns the value of attribute path.
-
#weight ⇒ Object
Returns the value of attribute weight.
Instance Method Summary collapse
-
#[](val) ⇒ Object
finds an ActionLink by matching the action.
-
#add(action, options = {}) ⇒ Object
(also: #<<)
adds an ActionLink, creating one from the arguments if need be.
- #add_separator(weight = 0) ⇒ Object
-
#add_to_group(link, group_name = nil) ⇒ Object
adds a link to a specific group groups are represented as a string separated by a dot eg member.crud.
- #add_to_set(link) ⇒ Object
- #click_menu? ⇒ Boolean
- #collect ⇒ Object
- #delete(val) ⇒ Object
- #delete_group(name) ⇒ Object
-
#each(options = {}, &block) ⇒ Object
iterates over the links, possibly by type.
- #find_duplicate(link) ⇒ Object
-
#initialize(name = :root, parent_path = nil) ⇒ ActionLinks
constructor
A new instance of ActionLinks.
- #method_missing(name, *args) ⇒ Object
- #respond_to_missing?(name) ⇒ Boolean
- #subgroup(name, label = nil) ⇒ Object
Constructor Details
#initialize(name = :root, parent_path = nil) ⇒ ActionLinks
Returns a new instance of ActionLinks.
14 15 16 17 18 19 20 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 14 def initialize(name = :root, parent_path = nil) @set = [] @name = name @css_class = name.to_s.downcase @weight = 0 @path = [parent_path, name].compact.join('.') unless name == :root end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 175 def method_missing(name, *args, &) return super if name.match?(/[=!?]$/) return subgroup(name.to_sym, args.first, &) if frozen? define_singleton_method name do |label = nil| value = instance_variable_get("@#{name}") unless value value = subgroup(name.to_sym, label) instance_variable_set("@#{name}", value) end yield value if block_given? value end send(name, args.first, &) end |
Instance Attribute Details
#click_menu=(value) ⇒ Object (writeonly)
Sets the attribute click_menu
11 12 13 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 11 def (value) = value end |
#css_class ⇒ Object
Returns the value of attribute css_class.
10 11 12 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 10 def css_class @css_class end |
#default_type ⇒ Object
Returns the value of attribute default_type.
10 11 12 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 10 def default_type @default_type end |
#label(record) ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 164 def label(record) case @label when Symbol ActiveScaffold::Registry.cache(:translations, @label) { as_(@label) } when Proc @label.call(record) else @label end end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 12 def name @name end |
#path ⇒ Object (readonly) Also known as: name_to_cache
Returns the value of attribute path.
12 13 14 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 12 def path @path end |
#weight ⇒ Object
Returns the value of attribute weight.
10 11 12 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 10 def weight @weight end |
Instance Method Details
#[](val) ⇒ Object
finds an ActionLink by matching the action
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 72 def [](val) links = [] @set.each do |item| next if item == :separator if item.is_a?(ActiveScaffold::DataStructures::ActionLinks) collected = item[val] links << collected unless collected.nil? elsif item.action.to_s == val.to_s links << item end end links.first end |
#add(action, options = {}) ⇒ Object Also known as: <<
adds an ActionLink, creating one from the arguments if need be
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 29 def add(action, = {}) link = if action.is_a?(ActiveScaffold::DataStructures::ActionLink) || action.is_a?(ActiveScaffold::DataStructures::ActionLinks) action else [:type] ||= default_type if default_type ActiveScaffold::DataStructures::ActionLink.new(action, ) end # NOTE: this duplicate check should be done by defining the comparison operator for an Action data structure existing = find_duplicate(link) if existing existing else # That s for backwards compatibility if we are in root of action_links # we have to move actionlink into members or collection subgroup group = (name == :root ? subgroup(link.type, link.type) : self) group.add_to_set(link) link end end |
#add_separator(weight = 0) ⇒ Object
52 53 54 55 56 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 52 def add_separator(weight = 0) raise 'Call add_separator on a group' if name == :root add_to_set ActionLinkSeparator.new(weight) end |
#add_to_group(link, group_name = nil) ⇒ Object
adds a link to a specific group groups are represented as a string separated by a dot eg member.crud
65 66 67 68 69 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 65 def add_to_group(link, group_name = nil) add_to = root add_to = group_name.split('.').inject(root) { |group, name| group.send(name) } if group_name add_to << link unless link.nil? end |
#add_to_set(link) ⇒ Object
58 59 60 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 58 def add_to_set(link) @set << link end |
#click_menu? ⇒ Boolean
22 23 24 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 22 def end |
#collect ⇒ Object
140 141 142 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 140 def collect @set end |
#delete(val) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 102 def delete(val) each(include_set: true) do |link, set| next if link == :separator if link.action.to_s == val.to_s set.delete link break end end end |
#delete_group(name) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 113 def delete_group(name) @set.each do |group| next unless group.is_a?(ActiveScaffold::DataStructures::ActionLinks) if group.name == name @set.delete group break else group.delete_group(name) end end end |
#each(options = {}, &block) ⇒ Object
iterates over the links, possibly by type
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 127 def each( = {}, &block) method = [:reverse] ? :reverse_each : :each @set.sort_by(&:weight).send(method) do |item| if item.is_a?(ActiveScaffold::DataStructures::ActionLinks) && ![:groups] item.each(, &block) elsif [:include_set] yield item, @set else yield item end end end |
#find_duplicate(link) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 87 def find_duplicate(link) links = [] @set.each do |item| next if item == :separator if item.is_a?(ActiveScaffold::DataStructures::ActionLinks) collected = item.find_duplicate(link) links << collected unless collected.nil? elsif item.action == link.action && item.static_controller? && item.controller == link.controller && item.parameters == link.parameters links << item end end links.first end |
#respond_to_missing?(name) ⇒ Boolean
191 192 193 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 191 def respond_to_missing?(name, *) name !~ /[=!?]$/ end |
#subgroup(name, label = nil) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/active_scaffold/data_structures/action_links.rb', line 146 def subgroup(name, label = nil) name = name.to_sym group = self if name == self.name group ||= @set.find do |item| name == item.name if item.is_a?(ActiveScaffold::DataStructures::ActionLinks) end if group.nil? raise FrozenError, "Can't add new subgroup '#{name}', links are frozen" if frozen? group = ActiveScaffold::DataStructures::ActionLinks.new(name, path) group.label = label || name group.default_type = self.name == :root ? (name if i[member collection].include?(name)) : default_type add_to_set group end group end |