Module: RocketCMS::Menu

Defined in:
lib/rocket_cms/menu.rb

Class Method Summary collapse

Class Method Details

.btn_jsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rocket_cms/menu.rb', line 14

def btn_js
  <<-END.strip_heredoc.gsub("\n", ' ').gsub(/ +/, ' ')
    var $t = $(this);
    $.ajax({
      type: "POST",
      url: $t.attr("href"),
      data: {ajax: true},
      success: function(r) {
        $t.attr("href", r.href);
        $t.removeClass("btn-success btn-danger");
        $t.addClass(r.class);
      },
      error: function(e) {
        alert(e.responseText);
      }
    });
    return false;
  END
end

.build_toggles(view, model, obj, btn_size, btn_style = '') ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rocket_cms/menu.rb', line 34

def build_toggles(view, model, obj, btn_size, btn_style = '')
  ret = []
  get_menus.each do |m|
    if RocketCMS.mongoid?
      on = obj.menu_ids.include?(BSON::ObjectId.from_string(m[:id]))
    else
      on = obj.menu_ids.include?(m[:id].to_i)
    end
    ret << view.link_to(
      m[:name],
      view.toggle_menu_path(model_name: model, id: obj.id, menu: m[:id], on: !on),
      #method: :post,
      title: m[:name],
      class: "btn btn-#{btn_size} #{on ? "btn-success" : "btn-danger"}",
      style: btn_style,
      onclick: btn_js
    )
  end
  ret
end

.get_menusObject



4
5
6
7
8
9
10
11
12
# File 'lib/rocket_cms/menu.rb', line 4

def get_menus
  Rails.cache.fetch 'menus', expires_in: 10.minutes do
    if RocketCMS.mongoid?
      ::Menu.all.map { |m| {id: m.id.to_s, name: m.name } }
    else
      ::Menu.all.map { |m| {id: m.id, name: m.name } }
    end
  end
end