Class: SimpleContentManagement::SimpleMenu
- Inherits:
-
Object
- Object
- SimpleContentManagement::SimpleMenu
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::Conversion
- Defined in:
- app/models/simple_content_management/simple_menu.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#submenu ⇒ Object
readonly
Returns the value of attribute submenu.
Class Method Summary collapse
- .all ⇒ Object
- .configure(&block) ⇒ Object
- .find(id) ⇒ Object
- .find_registered_menu_item(id) ⇒ Object
- .registered_menu_items ⇒ Object
- .registered_menus ⇒ Object
Instance Method Summary collapse
-
#initialize(id, options = {}) ⇒ SimpleMenu
constructor
A new instance of SimpleMenu.
- #persisted? ⇒ Boolean
- #simple_menu_items ⇒ Object
- #simple_menu_items_data ⇒ Object
- #simple_menu_items_data=(smis_data_string) ⇒ Object
- #update_attributes(attributes) ⇒ Object
Constructor Details
#initialize(id, options = {}) ⇒ SimpleMenu
Returns a new instance of SimpleMenu.
7 8 9 10 11 12 |
# File 'app/models/simple_content_management/simple_menu.rb', line 7 def initialize id, = {} @id = id @name = ["name"] @description = ["description"] @submenu = !!["submenu"] end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
2 3 4 |
# File 'app/models/simple_content_management/simple_menu.rb', line 2 def description @description end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
2 3 4 |
# File 'app/models/simple_content_management/simple_menu.rb', line 2 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
2 3 4 |
# File 'app/models/simple_content_management/simple_menu.rb', line 2 def name @name end |
#submenu ⇒ Object (readonly)
Returns the value of attribute submenu.
2 3 4 |
# File 'app/models/simple_content_management/simple_menu.rb', line 2 def @submenu end |
Class Method Details
.all ⇒ Object
81 82 83 84 85 |
# File 'app/models/simple_content_management/simple_menu.rb', line 81 def all @all ||= .map do |id, | self.new id, end end |
.configure(&block) ⇒ Object
99 100 101 |
# File 'app/models/simple_content_management/simple_menu.rb', line 99 def configure &block @registered_menu_items = SimpleContentManagement::SimpleMenuConfiguration.configure &block end |
.find(id) ⇒ Object
77 78 79 |
# File 'app/models/simple_content_management/simple_menu.rb', line 77 def find id all.find{ || .id == id } end |
.find_registered_menu_item(id) ⇒ Object
91 92 93 |
# File 'app/models/simple_content_management/simple_menu.rb', line 91 def id .find{ |smc| smc.id == id } end |
.registered_menu_items ⇒ Object
95 96 97 |
# File 'app/models/simple_content_management/simple_menu.rb', line 95 def @registered_menu_items || [] end |
.registered_menus ⇒ Object
87 88 89 |
# File 'app/models/simple_content_management/simple_menu.rb', line 87 def @registered_menus ||= YAML.load_file File.join Rails.root, "config", "menus.yml" end |
Instance Method Details
#persisted? ⇒ Boolean
72 73 74 |
# File 'app/models/simple_content_management/simple_menu.rb', line 72 def persisted? true end |
#simple_menu_items ⇒ Object
20 21 22 |
# File 'app/models/simple_content_management/simple_menu.rb', line 20 def SimpleContentManagement::SimpleMenuItem.where simple_menu_id: id, parent_id: nil end |
#simple_menu_items_data ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/simple_content_management/simple_menu.rb', line 24 def .includes(:children).sort_by(&:position).map{ |smi| { id: smi.id, is_target_blank: smi.is_target_blank, method: smi.method, name: smi.name, uri: smi.uri, children: smi.children.sort_by(&:position).map{ |ssmi| { id: ssmi.id, is_target_blank: ssmi.is_target_blank, method: ssmi.method, name: ssmi.name, uri: ssmi.uri } } } }.to_json end |
#simple_menu_items_data=(smis_data_string) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/simple_content_management/simple_menu.rb', line 39 def smis_data_string smis_data = JSON.parse smis_data_string existing_smis = SimpleContentManagement::SimpleMenuItem.where(simple_menu_id: id).all smis_data.each_with_index do |smi_data, position| smi_data["simple_menu_id"] = id smi_data["position"] = position ssmis_data = smi_data.delete "children" smi = nil if smi_data["id"].present? smi_data_id = smi_data.delete "id" smi = existing_smis.find{ |smi| smi.id == smi_data_id } smi.try :update_attributes, smi_data existing_smis.delete smi else smi = .create smi_data end ssmis_data.each_with_index do |ssmi_data, sposition| ssmi_data["simple_menu_id"] = id ssmi_data["position"] = sposition ssmi_data["parent_id"] = smi.id if ssmi_data["id"].present? ssmi_data_id = ssmi_data.delete "id" ssmi = existing_smis.find{ |ssmi| ssmi.id == ssmi_data_id } ssmi.try :update_attributes, ssmi_data existing_smis.delete ssmi else .create ssmi_data end end end existing_smis.each &:destroy end |
#update_attributes(attributes) ⇒ Object
14 15 16 17 18 |
# File 'app/models/simple_content_management/simple_menu.rb', line 14 def update_attributes attributes attributes.each do |k, v| self.send "#{k}=", v end end |