Skip to content

Commit

Permalink
Menu meta
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertandy committed Apr 25, 2017
1 parent 44b7b28 commit 52ceaee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
18 changes: 18 additions & 0 deletions app/models/menu_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@ class MenuItem < ActiveRecord::Base

default_scope { order(position: :asc, title: :asc) }

serialize :meta

def subnav?
menu_items.any?
end

def meta=(m)
self[:meta] = if m.is_a?(String)
YAML.load(m) rescue {}
else
m
end
end

def readable_meta
string = YAML.dump(meta)
string = string.sub(/^---[^\n]*\n/, "")
string = string.sub(/^\.\.\.\n/, "")

string
end
end
7 changes: 5 additions & 2 deletions app/views/admin/menus/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
<span class="js-handle handle-icon icon handle glyphicon glyphicon-resize-vertical"></span>
<%= i.add_nested_fields_link :menu_items, "+", class: "btn btn-info btn-xs" %>
</div>
<div class="col-md-5 col-lg-5 col-sm-5">
<div class="col-md-4 col-lg-5 col-sm-5">
<%= i.text_field :title, class: "form-control", placeholder: "The title of the item" %>
</div>
<div class="col-md-5 col-sm-5">
<div class="col-md-3 col-sm-5">
<%= i.text_field :url, class: "form-control", placeholder: "The URL - which should start with a /" %>
</div>
<div class="col-md-3 col-sm-5">
<%= i.text_area :meta, value: i.object.readable_meta, class: "form-control", placeholder: "Extra information. YAML-formatted" %>
</div>
<ul class="js-sortable sortable row">
<%= i.nested_fields_for :menu_items, wrapper_tag: :li, wrapper_options: { class: "col-xs-push-1 col-xs-11" } do |ff| %>
<%= ff.hidden_field :position, class: "js-position" %>
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20170425132742_add_meta_to_menu_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddMetaToMenuItems < ActiveRecord::Migration[5.0]
def change
change_table :menu_items do |t|
t.text :meta
end
end
end

0 comments on commit 52ceaee

Please sign in to comment.