app/models/spree/page.rb in spree_essential_cms-0.3.0.rc1 vs app/models/spree/page.rb in spree_essential_cms-0.4.0
- old
+ new
@@ -1,7 +1,17 @@
class Spree::Page < ActiveRecord::Base
+ class << self
+
+ def find_by_path(_path)
+ return super('/') if _path == "_home_" && self.exists?(:path => "/")
+ super _path.to_s.sub(/^\/*/, "/").gsub("--", "/")
+ end
+
+ end
+
+ attr_accessible :title, :path, :nav_title, :meta_title, :meta_description, :meta_keywords, :accessible, :visible
alias_attribute :name, :title
validates_presence_of :title
validates :path, :presence => true, :uniqueness => { :case_sensitive => false }
@@ -9,23 +19,18 @@
scope :active, where(:accessible => true)
scope :visible, active.where(:visible => true)
has_many :contents, :order => :position, :dependent => :destroy
- has_many :images, :as => :viewable, :class_name => 'Spree::PageImage', :order => :position, :dependent => :destroy
+ has_many :images, :as => :viewable, :class_name => "Spree::PageImage", :order => :position, :dependent => :destroy
before_validation :set_defaults
after_create :create_default_content
-
- def self.find_by_path(_path)
- return super('/') if _path == '_home_' && self.exists?(:path => '/')
- super _path.to_s.sub(/^\/*/, '/').gsub('--', '/')
- end
-
+
def to_param
- return '_home_' if path == '/'
- path.sub(/^\//, '').gsub('/', '--')
+ return "_home_" if path == "/"
+ path.sub(/^\/*/, "")
end
def meta_title
val = read_attribute(:meta_title)
val.blank? ? title : val
@@ -45,17 +50,23 @@
def root?
self.path == "/"
end
+ def path=(value)
+ value = value.to_s.strip
+ value.gsub!(/[\/\-\_]+$/, "") unless value == "/"
+ write_attribute :path, value
+ end
+
private
def set_defaults
return if title.blank?
#return errors.add(:path, "is reserved. Please use another") if path.to_s =~ /home/
self.nav_title = title if nav_title.blank?
self.path = nav_title.parameterize if path.blank?
- self.path = "/" + path.sub(/^\//, '')
+ self.path = "/" + path.sub(/^\//, "")
end
def create_default_content
self.contents.create(:title => title)
end
\ No newline at end of file