Class: ActiveScaffold::DataStructures::ActionLinks

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_scaffold/data_structures/action_links.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActionLinks

Returns a new instance of ActionLinks.



5
6
7
8
9
# File 'lib/active_scaffold/data_structures/action_links.rb', line 5

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



143
144
145
146
147
148
149
150
151
152
# File 'lib/active_scaffold/data_structures/action_links.rb', line 143

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

#labelObject



139
140
141
# File 'lib/active_scaffold/data_structures/action_links.rb', line 139

def label
  as_(@label) if @label
end

#nameObject

Returns the value of attribute name.



154
155
156
# File 'lib/active_scaffold/data_structures/action_links.rb', line 154

def name
  @name
end

#weightObject

Returns the value of attribute weight.



155
156
157
# File 'lib/active_scaffold/data_structures/action_links.rb', line 155

def weight
  @weight
end

Instance Method Details

#[](val) ⇒ Object

finds an ActionLink by matching the action



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_scaffold/data_structures/action_links.rb', line 46

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_scaffold/data_structures/action_links.rb', line 12

def add(action, options = {})
  link = if action.is_a?(ActiveScaffold::DataStructures::ActionLink) || action.is_a?(ActiveScaffold::DataStructures::ActionLinks)
    action
  else
    ActiveScaffold::DataStructures::ActionLink.new(action, options)
  end
  # NOTE: this duplicate check should be done by defining the comparison operator for an Action data structure
  existing = find_duplicate(link)
  unless existing
    # 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
  else
    existing
  end
end

#add_to_group(link, group = nil) ⇒ Object

adds a link to a specific group groups are represented as a string separated by a dot eg member.crud



39
40
41
42
43
# File 'lib/active_scaffold/data_structures/action_links.rb', line 39

def add_to_group(link, group = nil)
  add_to = root
  add_to = group.split('.').inject(root){|group, group_name| group.send(group_name)} if group
  add_to << link unless link.nil?
end

#add_to_set(link) ⇒ Object



32
33
34
# File 'lib/active_scaffold/data_structures/action_links.rb', line 32

def add_to_set(link)
  @set << link
end

#collectObject



115
116
117
# File 'lib/active_scaffold/data_structures/action_links.rb', line 115

def collect
  @set
end

#collect_by_type(type = nil) ⇒ Object



109
110
111
112
113
# File 'lib/active_scaffold/data_structures/action_links.rb', line 109

def collect_by_type(type = nil)
  links = []
  subgroup(type).each(type) {|link| links << link}
  links
end

#delete(val) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/active_scaffold/data_structures/action_links.rb', line 72

def delete(val)
  self.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



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/active_scaffold/data_structures/action_links.rb', line 81

def delete_group(name)
  @set.each do |group|
    if group.name == name
      @set.delete group
      break
    else
      group.delete_group(name)
      break
    end if group.is_a?(ActiveScaffold::DataStructures::ActionLinks)
  end
end

#each(options = {}, &block) ⇒ Object

iterates over the links, possibly by type



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_scaffold/data_structures/action_links.rb', line 94

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

Returns:

  • (Boolean)


119
120
121
# File 'lib/active_scaffold/data_structures/action_links.rb', line 119

def empty?
  @set.size == 0
end

#find_duplicate(link) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_scaffold/data_structures/action_links.rb', line 59

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 and item.static_controller? && item.controller == link.controller and item.parameters == link.parameters
    end
  end
  links.first
end

#subgroup(name, label = nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/active_scaffold/data_structures/action_links.rb', line 123

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
    add_to_set group
  end
  group
end