Sha256: 26f86fa0958c64669b0228cbe99c76453c63d89c3e8482d46d57016412061615

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 KB

Contents

class SimpleContentManagement::SimplePage < ActiveRecord::Base
	include SimpleContentManagement::ContentText
	include SimpleContentManagement::Breadcrumbs

	has_many :simple_routes, conditions: { deleted: false }, class_name: "SimpleContentManagement::SimpleRoute"
	belongs_to :preferred_menu_item, class_name: "SimpleContentManagement::SimpleMenuItem"

	attr_accessible :name, :title, :content_html, :simple_routes_list, :section, :preferred_menu_item_id, :meta_description, :meta_keywords
	scope :active, where(deleted: false)

	before_save :reconstruct_breadcrumbs

	def simple_menu_items
		SimpleContentManagement::SimpleMenuItem.where(uri: path_names.map{ |path| "/#{path}.html" } + (path_names.include?("prive") ? ["/"] : []))
	end

	def simple_routes_list
		path_names.join ","
	end

	def simple_routes_list=(routes_list)
		path_names = routes_list.split ","

		# destroy routes that are no longer required
		self.simple_routes.each do |simple_route|
			if path_names.include? simple_route.path
				path_names.delete simple_route.path
			else
				simple_route.destroy
			end
		end

		# ensure any existing routes are set active to the current page.
		if id.nil?
			SimpleContentManagement::SimpleRoute.where(path: path_names, deleted: false).update_all deleted: true
		else	
			SimpleContentManagement::SimpleRoute.where(path: path_names, deleted: false).where("simple_page_id <> ?", id).update_all deleted: true
		end

		# create the remaining routes
		path_names.each do |path_name|
			self.simple_routes.build path: path_name
		end
	end

	def path_names
		new_record? ? simple_routes.map(&:path) : simple_routes.active.pluck("path")
	end

	def destroy
		update_attribute :deleted, true
	end

	def label
		title
	end

	def reconstruct_breadcrumbs
		self.preferred_menu_item = simple_menu_items.first unless preferred_menu_item.present? || simple_menu_items.first.nil?
	end

	class << self
		def registered_content_pages
			@registered_content_pages ||= YAML.load_file File.join Rails.root, "config", "content_pages.yml"
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
th_simple_content_management-0.2.5 app/models/simple_content_management/simple_page.rb
th_simple_content_management-0.2.4 app/models/simple_content_management/simple_page.rb
th_simple_content_management-0.2.3 app/models/simple_content_management/simple_page.rb