app/models/alchemy/page.rb in alchemy_cms-2.2.4 vs app/models/alchemy/page.rb in alchemy_cms-2.3.rc5

- old
+ new

@@ -54,12 +54,12 @@ before_validation :set_url_name, :unless => proc { |page| page.systempage? || page.redirects_to_external? } before_save :set_title, :unless => proc { |page| page.systempage? || page.redirects_to_external? || !page.title.blank? } before_save :set_language_code, :unless => :systempage? before_save :set_restrictions_to_child_pages, :if => proc { |page| !page.systempage? && page.restricted_changed? } before_save :inherit_restricted_status, :if => proc { |page| !page.systempage? && page.parent && page.parent.restricted? } - after_create :autogenerate_elements, :unless => proc { |page| page.systempage? || page.do_not_autogenerate } after_create :create_cells, :unless => :systempage? + after_create :autogenerate_elements, :unless => proc { |page| page.systempage? || page.do_not_autogenerate } scope :language_roots, where(:language_root => true) scope :layoutpages, where(:layoutpage => true) scope :all_locked, where(:locked => true) scope :all_locked_by, lambda { |user| where(:locked => true, :locked_by => user.id) } @@ -139,16 +139,11 @@ def feed_elements elements.find_all_by_name(definition['feed_elements']) end def elements_grouped_by_cells - group = ::ActiveSupport::OrderedHash.new - self.cells.each { |cell| group[cell] = cell.elements.not_trashed } - if element_names_not_in_cell.any? - group[Cell.new({:name => 'for_other_elements'})] = elements.not_trashed.not_in_cell - end - return group + elements.not_trashed.in_cell.group_by(&:cell) end def element_names_from_cells cell_definitions.collect { |c| c['elements'] }.flatten.uniq end @@ -159,31 +154,29 @@ # Finds the previous page on the same structure level. Otherwise it returns nil. # Options: # => :restricted => boolean (standard: nil) - next restricted page (true), skip restricted pages (false), ignore restriction (nil) # => :public => boolean (standard: true) - next public page (true), skip public pages (false) - def previous_page(options = {}) - default_options = { + def previous(options = {}) + next_or_previous(:previous, { :restricted => nil, :public => true - } - options = default_options.merge(options) - find_next_or_previous_page("previous", options) + }.merge(options)) end + alias_method :previous_page, :previous # Finds the next page on the same structure level. Otherwise it returns nil. # Options: # => :restricted => boolean (standard: nil) - next restricted page (true), skip restricted pages (false), ignore restriction (nil) # => :public => boolean (standard: true) - next public page (true), skip public pages (false) - def next_page(options = {}) - default_options = { + def next(options = {}) + next_or_previous(:next, { :restricted => nil, :public => true - } - options = default_options.merge(options) - find_next_or_previous_page("next", options) + }.merge(options)) end + alias_method :next_page, :next def find_first_public(page) if (page.public == true) return page end @@ -370,11 +363,11 @@ self.public_was != self.public end def set_restrictions_to_child_pages descendants.each do |child| - child.update_attribute(:restricted, self.restricted?) + child.update_attributes(:restricted => self.restricted?) end end def inherit_restricted_status self.restricted = parent.restricted? @@ -425,11 +418,11 @@ new_element = Element.copy(element, :page_id => page.id, :cell_id => (cell.blank? ? nil : cell.id)) new_element.move_to_bottom end return page else - raise page.errors.full_messages + raise "`#{page.name}`: #{page.errors.full_messages}" end end # Gets the language_root page for page def get_language_root @@ -449,15 +442,15 @@ def self.find_or_create_layout_root_for(language_id) layoutroot = layout_root_for(language_id) return layoutroot if layoutroot language = Language.find(language_id) layoutroot = Page.new({ - :name => "Layoutroot for #{language.name}", - :layoutpage => true, - :language => language, - :do_not_autogenerate => true - }) + :name => "Layoutroot for #{language.name}", + :layoutpage => true, + :language => language, + :do_not_autogenerate => true + }) if layoutroot.save(:validate => false) layoutroot.move_to_child_of(Page.root) return layoutroot else raise "Layout root for #{language.name} could not be created" @@ -534,44 +527,56 @@ "alchemy/#{language_code}/#{urlname}" end private - def find_next_or_previous_page(direction = "next", options = {}) - if direction == "previous" - step_direction = ["pages.lft < ?", self.lft] + def next_or_previous(direction = :next, options = {}) + pages = self.class.scoped + if direction == :previous + step_direction = ["#{self.class.table_name}.lft < ?", self.lft] order_direction = "lft DESC" else - step_direction = ["pages.lft > ?", self.lft] + step_direction = ["#{self.class.table_name}.lft > ?", self.lft] order_direction = "lft" end - conditions = Page.merge_conditions( - {:parent_id => self.parent_id}, - {:public => options[:public]}, - step_direction - ) + pages = pages.where(:public => options[:public]) + pages = pages.where(:parent_id => self.parent_id) + pages = pages.where(step_direction) if !options[:restricted].nil? - conditions = Page.merge_conditions(conditions, {:restricted => options[:restricted]}) + pages = pages.where(:restricted => options[:restricted]) end - return Page.where(conditions).order(order_direction).limit(1) + pages.order(order_direction).limit(1).first end # Converts the given nbame into an url friendly string # Names shorter than 3 will be filled with dashes, so it does not collidate with the language code. def convert_url_name(name) url_name = name.gsub(/[äÄ]/, 'ae').gsub(/[üÜ]/, 'ue').gsub(/[öÖ]/, 'oe').parameterize url_name = ('-' * (3 - url_name.length)) + url_name if url_name.length < 3 return url_name end - # Looks in the layout_descripion, if there are elements to autogenerate. - # If so, it generates them. + # Looks in the page_layout descripion, if there are elements to autogenerate. + # + # And if so, it generates them. + # + # If the page has cells, it looks if there are elements to generate. + # def autogenerate_elements elements = self.layout_description["autogenerate"] - unless (elements.blank?) + if elements.present? elements.each do |element| - element = Element.create_from_scratch({'page_id' => self.id, 'name' => element}) - element.move_to_bottom if element + if self.has_cells? && (cell_definition = cell_definitions.detect { |c| c['elements'].include?(element) }) + cell = self.cells.find_by_name(cell_definition['name']) + if cell + attributes = {'page_id' => self.id, 'cell_id' => cell.id, 'name' => element} + else + raise "Cell not found for page #{self.inspect}" + end + else + attributes = {'page_id' => self.id, 'name' => element} + end + Element.create_from_scratch(attributes) end end end def set_language_code