Class: FatFreeCRM::ViewFactory

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/fat_free_crm/view_factory.rb

Overview

A view factory keeps track of views and the contexts in which they are available.


The context that a view is available for is defined by ‘controllers’ and ‘actions’

controllers => ['contacts'] means that the view is available when the contacts controller is used
actions => [:index, show] means that the view is available for search listings AND individual records
template => 'contacts/index_full' is the partial that is rendered for this view

Icon is optional. If specified, it will be passed to asset_path.

Constant Summary collapse

@@views =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ViewFactory

Instance methods




56
57
58
59
60
61
62
63
64
65
# File 'lib/fat_free_crm/view_factory.rb', line 56

def initialize(options = {})
  self.name = options[:name]
  self.title = options[:title]
  self.icon = options[:icon] # optional
  self.controllers = options[:controllers] || []
  self.actions = options[:actions] || []
  self.template = options[:template]
  self.id = generate_id
  self.class.register(self)
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



23
24
25
# File 'lib/fat_free_crm/view_factory.rb', line 23

def actions
  @actions
end

#controllersObject

Returns the value of attribute controllers.



23
24
25
# File 'lib/fat_free_crm/view_factory.rb', line 23

def controllers
  @controllers
end

#iconObject

Returns the value of attribute icon.



23
24
25
# File 'lib/fat_free_crm/view_factory.rb', line 23

def icon
  @icon
end

#idObject

Returns the value of attribute id.



23
24
25
# File 'lib/fat_free_crm/view_factory.rb', line 23

def id
  @id
end

#nameObject

Returns the value of attribute name.



23
24
25
# File 'lib/fat_free_crm/view_factory.rb', line 23

def name
  @name
end

#templateObject

Returns the value of attribute template.



23
24
25
# File 'lib/fat_free_crm/view_factory.rb', line 23

def template
  @template
end

#titleObject

Returns the value of attribute title.



23
24
25
# File 'lib/fat_free_crm/view_factory.rb', line 23

def title
  @title
end

Class Method Details

.register(view) ⇒ Object

Register with the view factory




30
31
32
# File 'lib/fat_free_crm/view_factory.rb', line 30

def register(view)
  @@views << view unless @@views.map(&:id).include?(view.id)
end

.template_for_current_view(options = {}) ⇒ Object

Return template name of the current view pass in options to specify view name




48
49
50
51
# File 'lib/fat_free_crm/view_factory.rb', line 48

def template_for_current_view(options = {})
  view = views_for(options).first
  view&.template
end

.views_for(options = {}) ⇒ Object

Return views that are available based on context




36
37
38
39
40
41
42
43
# File 'lib/fat_free_crm/view_factory.rb', line 36

def views_for(options = {})
  controller = options[:controller]
  action = options[:action]
  name = options[:name] # optional
  @@views.select do |view|
    view.controllers.include?(controller) && view.actions.include?(action) && (name.present? ? view.name == name : true)
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Define view equivalence. They are the same if they have the same id.




69
70
71
# File 'lib/fat_free_crm/view_factory.rb', line 69

def <=>(other)
  id <=> other.id
end