Class: SimpleContentManagement::SimpleMenu

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::Conversion
Defined in:
app/models/simple_content_management/simple_menu.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {}
	@id = id
	@name = options["name"]
	@description = options["description"]
	@submenu = !!options["submenu"]
end

Instance Attribute Details

#descriptionObject (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

#idObject (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

#nameObject (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

Returns the value of attribute submenu.



2
3
4
# File 'app/models/simple_content_management/simple_menu.rb', line 2

def submenu
  @submenu
end

Class Method Details

.allObject



81
82
83
84
85
# File 'app/models/simple_content_management/simple_menu.rb', line 81

def all
	@all ||= registered_menus.map do |id, options|
		self.new id, options
	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{ |simple_menu| simple_menu.id == id }
end

.find_registered_menu_item(id) ⇒ Object



91
92
93
# File 'app/models/simple_content_management/simple_menu.rb', line 91

def find_registered_menu_item id
	registered_menu_items.find{ |smc| smc.id == id }
end

.registered_menu_itemsObject



95
96
97
# File 'app/models/simple_content_management/simple_menu.rb', line 95

def registered_menu_items
	@registered_menu_items || []
end

.registered_menusObject



87
88
89
# File 'app/models/simple_content_management/simple_menu.rb', line 87

def registered_menus
	@registered_menus ||= YAML.load_file File.join Rails.root, "config", "menus.yml"
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/simple_content_management/simple_menu.rb', line 72

def persisted?
	true
end

#simple_menu_itemsObject



20
21
22
# File 'app/models/simple_content_management/simple_menu.rb', line 20

def simple_menu_items
	SimpleContentManagement::SimpleMenuItem.where simple_menu_id: id, parent_id: nil
end

#simple_menu_items_dataObject



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 simple_menu_items_data
	simple_menu_items.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 simple_menu_items_data= 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 = simple_menu_items.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
				simple_menu_items.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