Class: Goldencobra::Api::V2::NavigationMenusController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/goldencobra/api/v2/navigation_menus_controller.rb

Instance Method Summary collapse

Instance Method Details

#active_idsInteger

Get active IDs of current request/url Searches in Subtree of a given MasterElement Example localhost:3000/api/v2/navigation_menus/active?id=1&url=/seite1/seite-a

Parameters:

  • id (String)
    ID of Submenu to search for
  • url (String)

    Url to Search vor Active MenueIDs

Returns:

  • (Integer)

    Array if Integers als Menue IDs



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/goldencobra/api/v2/navigation_menus_controller.rb', line 19

def active_ids
  require "oj"
  require "addressable/uri"
  # @master_element is set by before filter

  if params[:url].present?
    url_to_search = params[:url]

    # URI parse domain and url path
    parsed_url = Addressable::URI.parse(url_to_search)

    current_menue = find_menu_with_matching_path(parsed_url)

    if current_menue.present?
      @active_menue_ids = current_menue.path_ids
    else
      @active_menue_ids = []
    end
  else
    @active_menue_ids = []
  end

  respond_to do |format|
    format.json { render json: Oj.dump(@active_menue_ids, mode: :compat), root: false }
  end
end

#indexjson

/api/v2/navigation_menus

[

{
    "id": 2,
    "title": "Startseite",
    "target": "/",
    "children": [
        {
            "id": 10,
            "title": "Seite 1",
            "target": "/seite1",
            "children": [
                {
                    "id": 12,
                    "title": "Subseite A",
                    "target": "/seite1/seite-a",
                    "children": []
                }
            ]
        },
        {
            "id": 11,
            "title": "Seite 2",
            "target": "/seite2",
            "children": []
        }
    ]
},
{
    "id": 5,
    "title": "Weiteres",
    "target": "/weiteres",
    "children": []
}

]

Response as JSON
{
   "active": true,
   "ancestry": null,
   "ancestry_depth": 0,
   "call_to_action_name": null,
   "created_at": "2014-10-10T09:43:54+02:00",
   "css_class": "",
   "description": null,
   "description_title": null,
   "id": 1,
   "image_id": null,
   "remote": false,
   "sorter": 0,
   "target": "",
   "title": "Top-Navigation",
   "updated_at": "2014-10-10T09:43:54+02:00"
}

Parameters:

  • id (String)
    ID of Submenu to render
  • methods (String)

    “css_class,liquid_description,navigation_image,description_title,call_to_action_name”

Returns:

  • (json)

    All menus with :id, :complete_list_name, etc.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/goldencobra/api/v2/navigation_menus_controller.rb', line 109

def index
  require "oj"
  # @master_element is set by before filter

  # Generate cache key: if any Menuitem is changed => invalidate
  last_modified = Goldencobra::Menue.order(:updated_at).pluck(:updated_at).last.to_i
  cache_sub_key = [
    params[:depth],
    params[:offset],
    display_methods,
    last_modified,
    Goldencobra::Menue.count
  ].flatten.join("_")
  cache_key = "Navigation/menue_#{@master_element.id}/#{Digest::MD5.hexdigest(cache_sub_key)}"

  #Gibt es das Menü bereits im Cache?
  if Rails.cache.exist?(cache_key)
    @json_tree = Rails.cache.read(cache_key)
  else
    # How many levels of subtree should be displayed
    depth = params[:depth].present? ? params[:depth].to_i : 9999

    # How many levels of subtree should be skipped
    offset = params[:offset].present? ? params[:offset].to_i : 0

    # Current Level of master element
    current_depth = @master_element.ancestry_depth

    # Get all menues of subtree from startlevel to endlevel
    menues = @master_element.subtree.active.visible
    menues = filter_elements(menues)
    menues = menues.after_depth(current_depth + offset).to_depth(current_depth + depth)

    # Prepare menu data to display
    menue_data_as_json = menues.arrange(order: :sorter)

    # JsonTree to display, use specified display_methods
    @json_tree = Goldencobra::Menue.json_tree(menue_data_as_json, display_methods)

    # Save result to cache
    Rails.cache.write(cache_key, @json_tree)
  end

  respond_to do |format|
    format.json { render json: Oj.dump(@json_tree, mode: :compat), root: false }
  end
end