Class: Backend::TimelineHelper::Timeline

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/backend/timeline_helper.rb

Defined Under Namespace

Classes: MarkerStep, Side, SideStep, Step

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ Timeline

Returns a new instance of Timeline.


131
132
133
134
135
136
137
# File 'app/helpers/backend/timeline_helper.rb', line 131

def initialize(object, options = {})
  @object = object
  @model = @object.class
  @sides = []
  @id = options[:id]
  @others = options[:others] || []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object


170
171
172
# File 'app/helpers/backend/timeline_helper.rb', line 170

def method_missing(method_name, *args)
  side(method_name.to_sym, *args)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id


22
23
24
# File 'app/helpers/backend/timeline_helper.rb', line 22

def id
  @id
end

#objectObject (readonly)

Returns the value of attribute object


22
23
24
# File 'app/helpers/backend/timeline_helper.rb', line 22

def object
  @object
end

#othersObject (readonly)

Returns the value of attribute others


22
23
24
# File 'app/helpers/backend/timeline_helper.rb', line 22

def others
  @others
end

#sidesObject (readonly)

Returns the value of attribute sides


22
23
24
# File 'app/helpers/backend/timeline_helper.rb', line 22

def sides
  @sides
end

Instance Method Details

#side(name, options = {}) ⇒ Object


156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/helpers/backend/timeline_helper.rb', line 156

def side(name, options = {})
  unless reflection = @model.reflect_on_association(name)
    raise ArgumentError, "Invalid reflection #{name.inspect} for #{@model.name}"
  end
  klass = reflection.class_name.constantize
  available_methods = klass.columns_hash.keys.map(&:to_sym)
  options[:label_method] ||= [:label, :name, :number, :coordinates, :id].detect { |m| available_methods.include?(m) } || :id
  options[:params] ||= {}
  options[:params][reflection.foreign_key.to_sym] ||= @object.id
  options[:params]["#{reflection.options[:as]}_type".to_sym] ||= @model.name if reflection.options[:as]
  options[:label] ||= @model.human_attribute_name(name)
  @sides << Side.new(self, name.to_sym, klass, options)
end

#stepsObject


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/helpers/backend/timeline_helper.rb', line 139

def steps
  list = []
  @sides.each do |side|
    list += side.steps if side.authorized?
  end
  now = Time.zone.now
  list << MarkerStep.new(now, :now) if list.detect { |s| s.at > now }
  count = 1
  ago = now - 1.year
  while list.detect { |s| s.at < ago }
    list << MarkerStep.new(ago, :past, 'datetime.distance_in_words.over_x_years'.t(count: count))
    count += 1
    ago = now - count.year
  end
  list.compact.sort.reverse
end