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
Returns a new instance of ActionLinks.
6
7
8
9
10
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 6
def initialize
@set = []
@name = :root
@weight = 0
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 149
def method_missing(name, *args, &block)
class_eval %{
def #{name}(label = nil)
@#{name} ||= subgroup('#{name}'.to_sym, label)
yield @#{name} if block_given?
@#{name}
end
}
send(name, args.first, &block)
end
|
Instance Attribute Details
#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
|
145
146
147
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 145
def label
as_(@label) if @label
end
|
Returns the value of attribute name.
160
161
162
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 160
def name
@name
end
|
Returns the value of attribute weight.
161
162
163
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 161
def weight
@weight
end
|
Instance Method Details
finds an ActionLink by matching the action
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 50
def [](val)
links = []
@set.each do |item|
if item.is_a?(ActiveScaffold::DataStructures::ActionLinks)
collected = item[val]
links << collected unless collected.nil?
else
links << item if item.action.to_s == val.to_s
end
end
links.first
end
|
#add(action, options = {}) ⇒ Object
Also known as:
<<
adds an ActionLink, creating one from the arguments if need be
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 13
def add(action, options = {})
link =
if action.is_a?(ActiveScaffold::DataStructures::ActionLink) || action.is_a?(ActiveScaffold::DataStructures::ActionLinks)
action
else
options[:type] ||= default_type if default_type
ActiveScaffold::DataStructures::ActionLink.new(action, options)
end
existing = find_duplicate(link)
if existing
existing
else
group = (name == :root ? subgroup(link.type, link.type) : self)
group.add_to_set(link)
link
end
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
43
44
45
46
47
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 43
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
36
37
38
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 36
def add_to_set(link)
@set << link
end
|
120
121
122
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 120
def collect
@set
end
|
#collect_by_type(type = nil) ⇒ Object
114
115
116
117
118
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 114
def collect_by_type(type = nil)
links = []
subgroup(type).each(type) { |link| links << link }
links
end
|
#delete(val) ⇒ Object
76
77
78
79
80
81
82
83
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 76
def delete(val)
each(:include_set => true) do |link, set|
if link.action.to_s == val.to_s
set.delete link
break
end
end
end
|
#delete_group(name) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 85
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)
break
end
end
end
|
#each(options = {}, &block) ⇒ Object
iterates over the links, possibly by type
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 99
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)
else
if options[:include_set]
yield item, @set
else
yield item
end
end
end
end
|
#empty? ⇒ Boolean
124
125
126
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 124
def empty?
@set.empty?
end
|
#find_duplicate(link) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 63
def find_duplicate(link)
links = []
@set.each do |item|
if item.is_a?(ActiveScaffold::DataStructures::ActionLinks)
collected = item.find_duplicate(link)
links << collected unless collected.nil?
else
links << item if item.action == link.action && item.static_controller? && item.controller == link.controller && item.parameters == link.parameters
end
end
links.first
end
|
#subgroup(name, label = nil) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/active_scaffold/data_structures/action_links.rb', line 128
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
group.label = label || name
group.name = 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
|