moving files around

Oleg committed Aug 22, 2011
commit a1af79b2cfb0832358f8931a84e150a077106e7c
Showing 13 changed files with 297 additions and 274 deletions
app/models/cms/file.rb +2 -0
@@ @@ -2,6 +2,8 @@ class Cms::File < ActiveRecord::Base
set_table_name :cms_files
+ cms_is_categorized
+
# -- AR Extensions --------------------------------------------------------
has_attached_file :file, ComfortableMexicanSofa.config.upload_file_options
app/models/cms/page.rb +1 -0
@@ @@ -3,6 +3,7 @@ class Cms::Page < ActiveRecord::Base
set_table_name :cms_pages
cms_acts_as_tree :counter_cache => :children_count
+ cms_is_categorized
cms_is_mirrored
cms_has_revisions_for :blocks_attributes
app/models/cms/snippet.rb +2 -0
@@ @@ -1,6 +1,8 @@
class Cms::Snippet < ActiveRecord::Base
set_table_name :cms_snippets
+
+ cms_is_categorized
cms_is_mirrored
cms_has_revisions_for :content
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb +6 -5
@@ @@ -7,16 +7,17 @@ end
'comfortable_mexican_sofa/error',
'comfortable_mexican_sofa/configuration',
'comfortable_mexican_sofa/http_auth',
- 'comfortable_mexican_sofa/rails_extensions',
'comfortable_mexican_sofa/controller_methods',
'comfortable_mexican_sofa/view_hooks',
'comfortable_mexican_sofa/view_methods',
'comfortable_mexican_sofa/form_builder',
- 'comfortable_mexican_sofa/acts_as_tree',
- 'comfortable_mexican_sofa/has_revisions',
- 'comfortable_mexican_sofa/is_mirrored',
'comfortable_mexican_sofa/tag',
- 'comfortable_mexican_sofa/fixtures'
+ 'comfortable_mexican_sofa/fixtures',
+ 'comfortable_mexican_sofa/extensions/rails',
+ 'comfortable_mexican_sofa/extensions/acts_as_tree',
+ 'comfortable_mexican_sofa/extensions/has_revisions',
+ 'comfortable_mexican_sofa/extensions/is_mirrored',
+ 'comfortable_mexican_sofa/extensions/is_categorized'
].each do |path|
require File.expand_path(path, File.dirname(__FILE__))
end
comfortable_mexican_sofa/acts_as_tree.rb b/lib/comfortable_mexican_sofa/acts_as_tree.rb +0 -102
@@ @@ -1,102 +0,0 @@
- module ComfortableMexicanSofa::ActsAsTree
-
- def self.included(base)
- base.extend(ClassMethods)
- end
-
- module ClassMethods
- def cms_acts_as_tree(options = {})
- configuration = {
- :foreign_key => 'parent_id',
- :order => nil,
- :counter_cache => nil,
- :dependent => :destroy,
- :touch => false }
- configuration.update(options) if options.is_a?(Hash)
-
- belongs_to :parent,
- :class_name => name,
- :foreign_key => configuration[:foreign_key],
- :counter_cache => configuration[:counter_cache],
- :touch => configuration[:touch]
-
- has_many :children,
- :class_name => name,
- :foreign_key => configuration[:foreign_key],
- :order => configuration[:order],
- :dependent => configuration[:dependent]
-
- class_eval <<-EOV
- include ComfortableMexicanSofa::ActsAsTree::InstanceMethods
-
- scope :roots,
- :conditions => "#{configuration[:foreign_key]} IS NULL",
- :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}
-
- def self.root
- roots.first
- end
-
- validates_each "#{configuration[:foreign_key]}" do |record, attr, value|
- if value
- if record.id == value
- record.errors.add attr, "cannot be it's own id"
- elsif record.descendants.map {|c| c.id}.include?(value)
- record.errors.add attr, "cannot be a descendant's id"
- end
- end
- end
-
- EOV
- end
- end
-
- module InstanceMethods
- # Returns list of ancestors, starting from parent until root.
- #
- # subchild1.ancestors # => [child1, root]
- def ancestors
- node, nodes = self, []
- nodes << node = node.parent while node.parent
- nodes
- end
-
- # Returns all children and children of children
- def descendants
- nodes = []
- self.children.each do |c|
- nodes << c
- nodes << c.descendants
- end
- nodes.flatten
- end
-
- # Returns the root node of the tree.
- def root
- node = self
- node = node.parent while node.parent
- node
- end
-
- # Checks if this node is a root
- def root?
- !self.parent
- end
-
- # Returns all siblings of the current node.
- #
- # subchild1.siblings # => [subchild2]
- def siblings
- self_and_siblings - [self]
- end
-
- # Returns all siblings and a reference to the current node.
- #
- # subchild1.self_and_siblings # => [subchild1, subchild2]
- def self_and_siblings
- parent ? parent.children : self.class.roots
- end
- end
- end
-
- ActiveRecord::Base.send :include, ComfortableMexicanSofa::ActsAsTree
\ No newline at end of file
comfortable_mexican_sofa/extensions/acts_as_tree.rb b/lib/comfortable_mexican_sofa/extensions/acts_as_tree.rb +102 -0
@@ @@ -0,0 +1,102 @@
+ module ComfortableMexicanSofa::ActsAsTree
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def cms_acts_as_tree(options = {})
+ configuration = {
+ :foreign_key => 'parent_id',
+ :order => nil,
+ :counter_cache => nil,
+ :dependent => :destroy,
+ :touch => false }
+ configuration.update(options) if options.is_a?(Hash)
+
+ belongs_to :parent,
+ :class_name => name,
+ :foreign_key => configuration[:foreign_key],
+ :counter_cache => configuration[:counter_cache],
+ :touch => configuration[:touch]
+
+ has_many :children,
+ :class_name => name,
+ :foreign_key => configuration[:foreign_key],
+ :order => configuration[:order],
+ :dependent => configuration[:dependent]
+
+ class_eval <<-EOV
+ include ComfortableMexicanSofa::ActsAsTree::InstanceMethods
+
+ scope :roots,
+ :conditions => "#{configuration[:foreign_key]} IS NULL",
+ :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}
+
+ def self.root
+ roots.first
+ end
+
+ validates_each "#{configuration[:foreign_key]}" do |record, attr, value|
+ if value
+ if record.id == value
+ record.errors.add attr, "cannot be it's own id"
+ elsif record.descendants.map {|c| c.id}.include?(value)
+ record.errors.add attr, "cannot be a descendant's id"
+ end
+ end
+ end
+
+ EOV
+ end
+ end
+
+ module InstanceMethods
+ # Returns list of ancestors, starting from parent until root.
+ #
+ # subchild1.ancestors # => [child1, root]
+ def ancestors
+ node, nodes = self, []
+ nodes << node = node.parent while node.parent
+ nodes
+ end
+
+ # Returns all children and children of children
+ def descendants
+ nodes = []
+ self.children.each do |c|
+ nodes << c
+ nodes << c.descendants
+ end
+ nodes.flatten
+ end
+
+ # Returns the root node of the tree.
+ def root
+ node = self
+ node = node.parent while node.parent
+ node
+ end
+
+ # Checks if this node is a root
+ def root?
+ !self.parent
+ end
+
+ # Returns all siblings of the current node.
+ #
+ # subchild1.siblings # => [subchild2]
+ def siblings
+ self_and_siblings - [self]
+ end
+
+ # Returns all siblings and a reference to the current node.
+ #
+ # subchild1.self_and_siblings # => [subchild1, subchild2]
+ def self_and_siblings
+ parent ? parent.children : self.class.roots
+ end
+ end
+ end
+
+ ActiveRecord::Base.send :include, ComfortableMexicanSofa::ActsAsTree
\ No newline at end of file
comfortable_mexican_sofa/extensions/has_revisions.rb b/lib/comfortable_mexican_sofa/extensions/has_revisions.rb +64 -0
@@ @@ -0,0 +1,64 @@
+ module ComfortableMexicanSofa::HasRevisions
+
+ def self.included(base)
+ base.send :extend, ClassMethods
+ end
+
+ module ClassMethods
+
+ def cms_has_revisions_for(*fields)
+
+ include ComfortableMexicanSofa::HasRevisions::InstanceMethods
+
+ attr_accessor :revision_data
+
+ has_many :revisions,
+ :as => :record,
+ :dependent => :destroy
+
+ before_save :prepare_revision
+ after_save :create_revision
+
+ define_method(:revision_fields) do
+ fields.collect(&:to_s)
+ end
+ end
+ end
+
+ module InstanceMethods
+
+ # Preparing revision data. A bit of a special thing to grab page blocks
+ def prepare_revision
+ return if self.new_record?
+ if (self.respond_to?(:blocks_attributes_changed) && self.blocks_attributes_changed) ||
+ !(self.changed & revision_fields).empty?
+ self.revision_data = revision_fields.inject({}) do |c, field|
+ c[field] = self.send("#{field}_was")
+ c
+ end
+ end
+ end
+
+ # Revision is created only if relevant data changed
+ def create_revision
+ return unless self.revision_data
+
+ # creating revision
+ if ComfortableMexicanSofa.config.revisions_limit.to_i != 0
+ self.revisions.create!(:data => self.revision_data)
+ end
+
+ # blowing away old revisions
+ ids = [0] + self.revisions.limit(ComfortableMexicanSofa.config.revisions_limit.to_i).collect(&:id)
+ self.revisions.where('id NOT IN (?)', ids).destroy_all
+ end
+
+ # Assigning whatever is found in revision data and attemptint to save the object
+ def restore_from_revision(revision)
+ return unless revision.record == self
+ self.update_attributes!(revision.data)
+ end
+ end
+ end
+
+ ActiveRecord::Base.send :include, ComfortableMexicanSofa::HasRevisions
\ No newline at end of file
comfortable_mexican_sofa/extensions/is_categorized.rb b/lib/comfortable_mexican_sofa/extensions/is_categorized.rb +17 -0
@@ @@ -0,0 +1,17 @@
+ module ComfortableMexicanSofa::IsCategorized
+
+ def self.included(base)
+ base.send :extend, ClassMethods
+ end
+
+ module ClassMethods
+ def cms_is_categorized
+ has_many :categories,
+ :through => :categorizations,
+ :as => :categorized
+ end
+ end
+
+ end
+
+ ActiveRecord::Base.send :include, ComfortableMexicanSofa::IsCategorized
\ No newline at end of file
comfortable_mexican_sofa/extensions/is_mirrored.rb b/lib/comfortable_mexican_sofa/extensions/is_mirrored.rb +81 -0
@@ @@ -0,0 +1,81 @@
+ module ComfortableMexicanSofa::IsMirrored
+
+ def self.included(base)
+ base.send :extend, ClassMethods
+ end
+
+ module ClassMethods
+ def cms_is_mirrored
+ include ComfortableMexicanSofa::IsMirrored::InstanceMethods
+
+ attr_accessor :is_mirrored
+
+ after_save :sync_mirror
+ after_destroy :destroy_mirror
+ end
+ end
+
+ module InstanceMethods
+
+ # Mirrors of the object found on other sites
+ def mirrors
+ return [] unless self.site.is_mirrored?
+ (Cms::Site.mirrored - [self.site]).collect do |site|
+ case self
+ when Cms::Layout then site.layouts.find_by_slug(self.slug)
+ when Cms::Page then site.pages.find_by_full_path(self.full_path)
+ when Cms::Snippet then site.snippets.find_by_slug(self.slug)
+ end
+ end.compact
+ end
+
+ # Creating or updating a mirror object. Relationships are mirrored
+ # but content is unique. When updating need to grab mirrors based on
+ # self.slug_was, new objects will use self.slug.
+ def sync_mirror
+ return if self.is_mirrored
+
+ (Cms::Site.mirrored - [self.site]).each do |site|
+ mirror = case self
+ when Cms::Layout
+ m = site.layouts.find_by_slug(self.slug_was || self.slug) || site.layouts.new
+ m.attributes = {
+ :slug => self.slug,
+ :parent_id => site.layouts.find_by_slug(self.parent.try(:slug)).try(:id)
+ }
+ m
+ when Cms::Page
+ m = site.pages.find_by_full_path(self.full_path_was || self.full_path) || site.pages.new
+ m.attributes = {
+ :slug => self.slug,
+ :label => self.slug.blank?? self.label : m.label,
+ :parent_id => site.pages.find_by_full_path(self.parent.try(:full_path)).try(:id),
+ :layout => site.layouts.find_by_slug(self.layout.slug)
+ }
+ m
+ when Cms::Snippet
+ m = site.snippets.find_by_slug(self.slug_was || self.slug) || site.snippets.new
+ m.attributes = {
+ :slug => self.slug
+ }
+ m
+ end
+
+ mirror.is_mirrored = true
+ mirror.save!
+ end
+ end
+
+ # Mirrors should be destroyed
+ def destroy_mirror
+ return if self.is_mirrored
+ mirrors.each do |mirror|
+ mirror.is_mirrored = true
+ mirror.destroy
+ end
+ end
+ end
+
+ end
+
+ ActiveRecord::Base.send :include, ComfortableMexicanSofa::IsMirrored
\ No newline at end of file
comfortable_mexican_sofa/extensions/rails.rb b/lib/comfortable_mexican_sofa/extensions/rails.rb +22 -0
@@ @@ -0,0 +1,22 @@
+ class String
+ # Converts string to something suitable to be used as an element id
+ def idify
+ self.strip.gsub(/\W/, '_').gsub(/\s|^_*|_*$/, '').squeeze('_')
+ end
+
+ # Capitalize all words in the string
+ def capitalize_all(delimiter = ' ')
+ self.split(delimiter).collect{|w| w.capitalize }.join(' ')
+ end
+ end
+
+ module Enumerable
+ # Like a normal collect, only with index
+ def collect_with_index
+ result = []
+ self.each_with_index do |elt, idx|
+ result << yield(elt, idx)
+ end
+ result
+ end
+ end
\ No newline at end of file
comfortable_mexican_sofa/has_revisions.rb b/lib/comfortable_mexican_sofa/has_revisions.rb +0 -64
@@ @@ -1,64 +0,0 @@
- module ComfortableMexicanSofa::HasRevisions
-
- def self.included(base)
- base.send :extend, ClassMethods
- end
-
- module ClassMethods
-
- def cms_has_revisions_for(*fields)
-
- include ComfortableMexicanSofa::HasRevisions::InstanceMethods
-
- attr_accessor :revision_data
-
- has_many :revisions,
- :as => :record,
- :dependent => :destroy
-
- before_save :prepare_revision
- after_save :create_revision
-
- define_method(:revision_fields) do
- fields.collect(&:to_s)
- end
- end
- end
-
- module InstanceMethods
-
- # Preparing revision data. A bit of a special thing to grab page blocks
- def prepare_revision
- return if self.new_record?
- if (self.respond_to?(:blocks_attributes_changed) && self.blocks_attributes_changed) ||
- !(self.changed & revision_fields).empty?
- self.revision_data = revision_fields.inject({}) do |c, field|
- c[field] = self.send("#{field}_was")
- c
- end
- end
- end
-
- # Revision is created only if relevant data changed
- def create_revision
- return unless self.revision_data
-
- # creating revision
- if ComfortableMexicanSofa.config.revisions_limit.to_i != 0
- self.revisions.create!(:data => self.revision_data)
- end
-
- # blowing away old revisions
- ids = [0] + self.revisions.limit(ComfortableMexicanSofa.config.revisions_limit.to_i).collect(&:id)
- self.revisions.where('id NOT IN (?)', ids).destroy_all
- end
-
- # Assigning whatever is found in revision data and attemptint to save the object
- def restore_from_revision(revision)
- return unless revision.record == self
- self.update_attributes!(revision.data)
- end
- end
- end
-
- ActiveRecord::Base.send :include, ComfortableMexicanSofa::HasRevisions
\ No newline at end of file
comfortable_mexican_sofa/is_mirrored.rb b/lib/comfortable_mexican_sofa/is_mirrored.rb +0 -81
@@ @@ -1,81 +0,0 @@
- module ComfortableMexicanSofa::IsMirrored
-
- def self.included(base)
- base.send :extend, ClassMethods
- end
-
- module ClassMethods
- def cms_is_mirrored
- include ComfortableMexicanSofa::IsMirrored::InstanceMethods
-
- attr_accessor :is_mirrored
-
- after_save :sync_mirror
- after_destroy :destroy_mirror
- end
- end
-
- module InstanceMethods
-
- # Mirrors of the object found on other sites
- def mirrors
- return [] unless self.site.is_mirrored?
- (Cms::Site.mirrored - [self.site]).collect do |site|
- case self
- when Cms::Layout then site.layouts.find_by_slug(self.slug)
- when Cms::Page then site.pages.find_by_full_path(self.full_path)
- when Cms::Snippet then site.snippets.find_by_slug(self.slug)
- end
- end.compact
- end
-
- # Creating or updating a mirror object. Relationships are mirrored
- # but content is unique. When updating need to grab mirrors based on
- # self.slug_was, new objects will use self.slug.
- def sync_mirror
- return if self.is_mirrored
-
- (Cms::Site.mirrored - [self.site]).each do |site|
- mirror = case self
- when Cms::Layout
- m = site.layouts.find_by_slug(self.slug_was || self.slug) || site.layouts.new
- m.attributes = {
- :slug => self.slug,
- :parent_id => site.layouts.find_by_slug(self.parent.try(:slug)).try(:id)
- }
- m
- when Cms::Page
- m = site.pages.find_by_full_path(self.full_path_was || self.full_path) || site.pages.new
- m.attributes = {
- :slug => self.slug,
- :label => self.slug.blank?? self.label : m.label,
- :parent_id => site.pages.find_by_full_path(self.parent.try(:full_path)).try(:id),
- :layout => site.layouts.find_by_slug(self.layout.slug)
- }
- m
- when Cms::Snippet
- m = site.snippets.find_by_slug(self.slug_was || self.slug) || site.snippets.new
- m.attributes = {
- :slug => self.slug
- }
- m
- end
-
- mirror.is_mirrored = true
- mirror.save!
- end
- end
-
- # Mirrors should be destroyed
- def destroy_mirror
- return if self.is_mirrored
- mirrors.each do |mirror|
- mirror.is_mirrored = true
- mirror.destroy
- end
- end
- end
-
- end
-
- ActiveRecord::Base.send :include, ComfortableMexicanSofa::IsMirrored
\ No newline at end of file
comfortable_mexican_sofa/rails_extensions.rb b/lib/comfortable_mexican_sofa/rails_extensions.rb +0 -22
@@ @@ -1,22 +0,0 @@
- class String
- # Converts string to something suitable to be used as an element id
- def idify
- self.strip.gsub(/\W/, '_').gsub(/\s|^_*|_*$/, '').squeeze('_')
- end
-
- # Capitalize all words in the string
- def capitalize_all(delimiter = ' ')
- self.split(delimiter).collect{|w| w.capitalize }.join(' ')
- end
- end
-
- module Enumerable
- # Like a normal collect, only with index
- def collect_with_index
- result = []
- self.each_with_index do |elt, idx|
- result << yield(elt, idx)
- end
- result
- end
- end
\ No newline at end of file