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




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

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.



21
22
23
# File 'lib/fat_free_crm/view_factory.rb', line 21

def actions
  @actions
end

#controllersObject

Returns the value of attribute controllers.



21
22
23
# File 'lib/fat_free_crm/view_factory.rb', line 21

def controllers
  @controllers
end

#iconObject

Returns the value of attribute icon.



21
22
23
# File 'lib/fat_free_crm/view_factory.rb', line 21

def icon
  @icon
end

#idObject

Returns the value of attribute id.



21
22
23
# File 'lib/fat_free_crm/view_factory.rb', line 21

def id
  @id
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/fat_free_crm/view_factory.rb', line 21

def name
  @name
end

#templateObject

Returns the value of attribute template.



21
22
23
# File 'lib/fat_free_crm/view_factory.rb', line 21

def template
  @template
end

#titleObject

Returns the value of attribute title.



21
22
23
# File 'lib/fat_free_crm/view_factory.rb', line 21

def title
  @title
end

Class Method Details

.register(view) ⇒ Object

Register with the view factory




28
29
30
# File 'lib/fat_free_crm/view_factory.rb', line 28

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




46
47
48
49
# File 'lib/fat_free_crm/view_factory.rb', line 46

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

.views_for(options = {}) ⇒ Object

Return views that are available based on context




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

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.




67
68
69
# File 'lib/fat_free_crm/view_factory.rb', line 67

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