Class: ActiveScaffold::DataStructures::ActionLinks
- Inherits:
-
Object
- Object
- ActiveScaffold::DataStructures::ActionLinks
show all
- Includes:
- Enumerable
- Defined in:
- lib/active_scaffold/data_structures/action_links.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name = :root) ⇒ ActionLinks
Returns a new instance of ActionLinks.
6
7
8
9
10
11
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 6
def initialize(name = :root)
@set = []
@name = name
@css_class = name.to_s.downcase
@weight = 0
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 159
def method_missing(name, *args, &block)
return super if name.match?(/[!?]$/)
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{name}(label = nil) # rubocop:disable Style/CommentedKeyword
@#{name} ||= subgroup('#{name}'.to_sym, label)
yield @#{name} if block_given?
@#{name}
end
METHOD
send(name, args.first, &block)
end
|
Instance Attribute Details
#css_class ⇒ Object
Returns the value of attribute css_class.
177
178
179
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 177
def css_class
@css_class
end
|
#default_type ⇒ Object
Returns the value of attribute default_type.
4
5
6
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 4
def default_type
@default_type
end
|
#label(record) ⇒ Object
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 148
def label(record)
case @label
when Symbol
ActiveScaffold::Registry.cache(:translations, @label) { as_(@label) }
when Proc
@label.call(record)
else
@label
end
end
|
Returns the value of attribute name.
175
176
177
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 175
def name
@name
end
|
Returns the value of attribute weight.
176
177
178
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 176
def weight
@weight
end
|
Instance Method Details
finds an ActionLink by matching the action
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 57
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
#add_separator(weight = 0) ⇒ Object
37
38
39
40
41
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 37
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
50
51
52
53
54
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 50
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
43
44
45
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 43
def add_to_set(link)
@set << link
end
|
124
125
126
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 124
def collect
@set
end
|
#delete(val) ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 87
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
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 98
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
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 111
def each(options = {}, &block)
method = options[:reverse] ? :reverse_each : :each
@set.sort_by(&:weight).send(method) do |item|
if item.is_a?(ActiveScaffold::DataStructures::ActionLinks) && !options[:groups]
item.each(options, &block)
elsif options[:include_set]
yield item, @set
else
yield item
end
end
end
|
#empty? ⇒ Boolean
128
129
130
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 128
def empty?
@set.empty?
end
|
#find_duplicate(link) ⇒ Object
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 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
171
172
173
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 171
def respond_to_missing?(name, *)
name !~ /[!?]$/
end
|
#subgroup(name, label = nil) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 132
def subgroup(name, label = nil)
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?
group = ActiveScaffold::DataStructures::ActionLinks.new(name)
group.label = label || name
group.default_type = self.name == :root ? (name.to_sym if %w[member collection].include?(name.to_s)) : default_type
add_to_set group
end
group
end
|