Class: Refinery::Menus::MenuLink
- Inherits:
-
Core::BaseModel
- Object
- Core::BaseModel
- Refinery::Menus::MenuLink
- Defined in:
- app/models/refinery/menus/menu_link.rb
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #custom_link? ⇒ Boolean
- #resource ⇒ Object
- #resource_config ⇒ Object
- #resource_klass ⇒ Object
- #resource_link? ⇒ Boolean
- #resource_title ⇒ Object
- #resource_type ⇒ Object
- #resource_url ⇒ Object
- #set_label ⇒ Object
- #title ⇒ Object
- #to_refinery_menu_item ⇒ Object
- #type_name ⇒ Object
- #url ⇒ Object
Class Method Details
.find_all_of_type(type) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'app/models/refinery/menus/menu_link.rb', line 21 def self.find_all_of_type(type) # find all resources of the given type, determined by the configuration # TODO - we may want to allow configuration of conditions (DONE), ordering, etc if scope = self.resource_config(type)[:scope] scope.is_a?(Symbol) ? resource_klass(type).send(scope) : resource_klass(type).instance_eval(&scope) else resource_klass(type).all end end |
.resource_config(type) ⇒ Object
35 36 37 |
# File 'app/models/refinery/menus/menu_link.rb', line 35 def self.resource_config(type) Refinery::Menus.[type.to_sym] end |
.resource_klass(type) ⇒ Object
31 32 33 |
# File 'app/models/refinery/menus/menu_link.rb', line 31 def self.resource_klass(type) resource_config(type)[:klass].constantize end |
Instance Method Details
#as_json(options = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/models/refinery/menus/menu_link.rb', line 102 def as_json(={}) json = super() if resource_link? json = { resource: { title: resource_title } }.merge(json) end json end |
#custom_link? ⇒ Boolean
69 70 71 |
# File 'app/models/refinery/menus/menu_link.rb', line 69 def custom_link? resource_id.nil? || resource_type.nil? end |
#resource ⇒ Object
77 78 79 80 |
# File 'app/models/refinery/menus/menu_link.rb', line 77 def resource return nil if custom_link? resource_klass.find(resource_id) end |
#resource_config ⇒ Object
57 58 59 |
# File 'app/models/refinery/menus/menu_link.rb', line 57 def resource_config Refinery::Menus::MenuLink.resource_config(resource_type) end |
#resource_klass ⇒ Object
53 54 55 |
# File 'app/models/refinery/menus/menu_link.rb', line 53 def resource_klass Refinery::Menus::MenuLink.resource_klass(resource_type) end |
#resource_link? ⇒ Boolean
73 74 75 |
# File 'app/models/refinery/menus/menu_link.rb', line 73 def resource_link? resource_id.present? && resource_type.present? end |
#resource_title ⇒ Object
82 83 84 |
# File 'app/models/refinery/menus/menu_link.rb', line 82 def resource_title resource.send(resource_config[:title_attr]) end |
#resource_type ⇒ Object
61 62 63 |
# File 'app/models/refinery/menus/menu_link.rb', line 61 def resource_type super || "Custom link" end |
#resource_url ⇒ Object
90 91 92 |
# File 'app/models/refinery/menus/menu_link.rb', line 90 def resource_url resource.present? ? resource.url : '/' end |
#set_label ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/refinery/menus/menu_link.rb', line 39 def set_label if label.blank? if custom_link? begin self.label = custom_url.match(/(\w+)\.\w+$/).captures.join.titleize rescue self.label = custom_url end else self.label = resource.send(resource_config[:title_attr]) end end end |
#title ⇒ Object
86 87 88 |
# File 'app/models/refinery/menus/menu_link.rb', line 86 def title title_attribute.present? ? title_attribute : label end |
#to_refinery_menu_item ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/models/refinery/menus/menu_link.rb', line 114 def { :id => id, :lft => lft, :menu_match => , :parent_id => parent_id, :rgt => rgt, :title => label, :type => self.class.name, :url => url, :html => { :id => id_attribute, :class => class_attribute, :title => title } } end |
#type_name ⇒ Object
65 66 67 |
# File 'app/models/refinery/menus/menu_link.rb', line 65 def type_name resource_type.titleize end |
#url ⇒ Object
94 95 96 97 98 99 100 |
# File 'app/models/refinery/menus/menu_link.rb', line 94 def url if custom_link? custom_url else resource_url end end |