Class: Goldencobra::Menue

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/goldencobra/menue.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.filtered_methods(method_names = []) ⇒ Array

Filtert die übergebenen mthodennamen anhand einer Whiteliste und ersetzt exteren methodenbezeichnungen mit internene helpern

description => liquid_description

Parameters:

  • method_names (Array) (defaults to: [])

    Liste an Methodennamen als String

Returns:

  • (Array)

    Liste an methoden als Symbol, bereinigt von invaliden aufrufen



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/goldencobra/menue.rb', line 124

def self.filtered_methods(method_names=[])
  #Alle zugelasssenen methodennamen als Array of Strings
  allowed_attribute_methods = Goldencobra::Menue.new.attributes.keys
  additional_whitelist_methods = ["liquid_description", "navigation_image", "article_id"]
  all_allowed_methodes = allowed_attribute_methods + additional_whitelist_methods

  #Filter übergebene methodenliste auf die zugelassenen namen
  selected_methods = method_names.select{ |a| all_allowed_methodes.include?(a) }

  #Wandle method names um in Symbols
  method_names = selected_methods.map{ |a| a.to_sym }

  return method_names.sort
end

.find_by_pathname(name) ⇒ Object



63
64
65
66
67
68
69
# File 'app/models/goldencobra/menue.rb', line 63

def self.find_by_pathname(name)
  if name.include?("/")
    where(title: name.split("/").last).select{|a| a.path.map(&:title).join("/") == name}.first
  else
    find_by_title(name)
  end
end

.json_tree(nodes, display_methods) ⇒ Hash

Liefert ein Hash der übegenene Anestry Arranged Daten }

Parameters:

  • nodes (Goldencobra::Menue)

    Ein Eltern-Menüelement

  • display_methods (:method)

    Optionale Liste an Mehtoden die auf das Menüelement aufgerufen werden sollen

    {

    id: node.id,
    title: node.title,
    target: node.target,
    eg_desciption: node.eg_description
    children: json_tree(sub_nodes).compact
    

Returns:

  • (Hash)

    Hash of Menue Data with optional attributes



152
153
154
155
156
# File 'app/models/goldencobra/menue.rb', line 152

def self.json_tree(nodes, display_methods )
  nodes.map do |node, sub_nodes|
    node.as_json(only: [:id, :title, :target], methods: display_methods).merge({children: json_tree(sub_nodes, display_methods).compact})
  end
end

Instance Method Details

#article_idObject



95
96
97
# File 'app/models/goldencobra/menue.rb', line 95

def article_id
  Goldencobra::Article.select([:url_name, :startpage, :ancestry, :id, :url_path]).select{ |a| a.public_url == target }.first.try(:id)
end

#has_active_child?(request, subtree_menues) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'app/models/goldencobra/menue.rb', line 76

def has_active_child?(request, subtree_menues)
  @has_active_child_result ||= {}
  @has_active_child_result[request.path.squeeze("/").split("?")[0]] ||= has_active_descendant?(subtree_menues, request)
end

#has_active_descendant?(subtree_menues, request) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/goldencobra/menue.rb', line 81

def has_active_descendant?(subtree_menues,request)
  subtree_menues.select{|a| a.ancestry.to_s.starts_with?("#{self.ancestry}/#{self.id}")}.map(&:target).include?(request.path.squeeze("/").split("?")[0])
end

#has_childrenObject



85
86
87
# File 'app/models/goldencobra/menue.rb', line 85

def has_children
  self.has_children?
end

#is_active?(request) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'app/models/goldencobra/menue.rb', line 71

def is_active?(request)
  @is_active_result ||= {}
  @is_active_result[request.path.squeeze("/").chomp("/").split("?")[0]] ||= request.path.squeeze("/").chomp("/").split("?")[0] == self.target.gsub("\"",'')
end

#liquid_descriptionString

Render Content of Description with liquid Tags

Returns:

  • (String)

    self.description liquified



102
103
104
105
# File 'app/models/goldencobra/menue.rb', line 102

def liquid_description
  template = Liquid::Template.parse(self.description)
  template.render(Goldencobra::Article::LiquidParser)
end

#mapped_to_article?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'app/models/goldencobra/menue.rb', line 89

def mapped_to_article?
  # TODO: Anfrage dauert zu lange bei 2000 Artikeln
  # @mapped_to_article_result ||= Goldencobra::Article.select([:url_name, :startpage, :ancestry, :id]).map{|a| a.public_url}.uniq.include?(self.target)
  false
end


107
108
109
110
111
112
113
114
# File 'app/models/goldencobra/menue.rb', line 107

def navigation_image
  return {} unless self.image

  {
    "alt_text" => self.image.alt_text ? self.image.alt_text : self.image.image_file_name,
    "src" => self.image.image(:original)
  }
end