Class: Refinery::Menus::MenuLink

Inherits:
Core::BaseModel
  • Object
show all
Defined in:
app/models/refinery/menus/menu_link.rb

Class Method Summary collapse

Instance Method Summary collapse

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.menu_resources[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(options={})
  json = super(options)
  if resource_link?
    json = {
      resource: {
        title: resource_title
      }
    }.merge(json)
  end
  json
end

#custom_link?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/models/refinery/menus/menu_link.rb', line 69

def custom_link?
  resource_id.nil? || resource_type.nil?
end

#resourceObject



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_configObject



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_klassObject



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

Returns:

  • (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_titleObject



82
83
84
# File 'app/models/refinery/menus/menu_link.rb', line 82

def resource_title
  resource.send(resource_config[:title_attr])
end

#resource_typeObject



61
62
63
# File 'app/models/refinery/menus/menu_link.rb', line 61

def resource_type
  super || "Custom link"
end

#resource_urlObject



90
91
92
# File 'app/models/refinery/menus/menu_link.rb', line 90

def resource_url
  resource.present? ? resource.url : '/'
end

#set_labelObject



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

#titleObject



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_itemObject



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 to_refinery_menu_item
  {
    :id => id,
    :lft => lft,
    :menu_match => 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_nameObject



65
66
67
# File 'app/models/refinery/menus/menu_link.rb', line 65

def type_name
  resource_type.titleize
end

#urlObject



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