From fa538bccd3c85ed40be3bf55c63f0c23753402b3 Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 29 Dec 2006 17:45:39 +0000 Subject: Moving the tagging stuff to an "external" directory, instead of "lib". git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1991 980ebf18-57e1-0310-9a29-db15c13687c0 --- lib/puppet/rails/external/tagging/README | 4 ++ .../rails/external/tagging/acts_as_taggable.rb | 62 ++++++++++++++++++++++ lib/puppet/rails/external/tagging/init.rb | 5 ++ lib/puppet/rails/external/tagging/tag.rb | 50 +++++++++++++++++ lib/puppet/rails/external/tagging/tagging.rb | 12 +++++ lib/puppet/rails/lib/README | 4 -- lib/puppet/rails/lib/acts_as_taggable.rb | 62 ---------------------- lib/puppet/rails/lib/init.rb | 5 -- lib/puppet/rails/lib/tag.rb | 50 ----------------- lib/puppet/rails/lib/tagging.rb | 12 ----- lib/puppet/rails/resource.rb | 2 +- 11 files changed, 134 insertions(+), 134 deletions(-) create mode 100644 lib/puppet/rails/external/tagging/README create mode 100644 lib/puppet/rails/external/tagging/acts_as_taggable.rb create mode 100644 lib/puppet/rails/external/tagging/init.rb create mode 100644 lib/puppet/rails/external/tagging/tag.rb create mode 100644 lib/puppet/rails/external/tagging/tagging.rb delete mode 100644 lib/puppet/rails/lib/README delete mode 100644 lib/puppet/rails/lib/acts_as_taggable.rb delete mode 100644 lib/puppet/rails/lib/init.rb delete mode 100644 lib/puppet/rails/lib/tag.rb delete mode 100644 lib/puppet/rails/lib/tagging.rb (limited to 'lib') diff --git a/lib/puppet/rails/external/tagging/README b/lib/puppet/rails/external/tagging/README new file mode 100644 index 000000000..8d2f90822 --- /dev/null +++ b/lib/puppet/rails/external/tagging/README @@ -0,0 +1,4 @@ +Acts As Taggable +================= + +Allows for tags to be added to multiple classes. \ No newline at end of file diff --git a/lib/puppet/rails/external/tagging/acts_as_taggable.rb b/lib/puppet/rails/external/tagging/acts_as_taggable.rb new file mode 100644 index 000000000..137a1c2a8 --- /dev/null +++ b/lib/puppet/rails/external/tagging/acts_as_taggable.rb @@ -0,0 +1,62 @@ +module ActiveRecord + module Acts #:nodoc: + module Taggable #:nodoc: + def self.included(base) + base.extend(ClassMethods) + end + + module ClassMethods + def acts_as_taggable(options = {}) + write_inheritable_attribute(:acts_as_taggable_options, { + :taggable_type => ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s, + :from => options[:from] + }) + + class_inheritable_reader :acts_as_taggable_options + + has_many :taggings, :as => :taggable, :dependent => true + has_many :tags, :through => :taggings + + include ActiveRecord::Acts::Taggable::InstanceMethods + extend ActiveRecord::Acts::Taggable::SingletonMethods + end + end + + module SingletonMethods + def find_tagged_with(list) + find_by_sql([ + "SELECT #{table_name}.* FROM #{table_name}, tags, taggings " + + "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " + + "AND taggings.taggable_type = ? " + + "AND taggings.tag_id = tags.id AND tags.name IN (?)", + acts_as_taggable_options[:taggable_type], list + ]) + end + def tags(options = {}) + options.merge!(:taggable_type => self.to_s) + Tag.tags(options) + end + end + + module InstanceMethods + def tag_with(list) + Tag.transaction do + taggings.destroy_all + + Tag.parse(list).each do |name| + if acts_as_taggable_options[:from] + send(acts_as_taggable_options[:from]).tags.find_or_create_by_name(name).on(self) + else + Tag.find_or_create_by_name(name).on(self) + end + end + end + end + + def tag_list + tags.collect { |tag| tag.name.include?(" ") ? "'#{tag.name}'" : tag.name }.join(" ") + end + end + end + end +end diff --git a/lib/puppet/rails/external/tagging/init.rb b/lib/puppet/rails/external/tagging/init.rb new file mode 100644 index 000000000..5069d8040 --- /dev/null +++ b/lib/puppet/rails/external/tagging/init.rb @@ -0,0 +1,5 @@ +require 'puppet/rails/external/tagging/acts_as_taggable' +ActiveRecord::Base.send(:include, ActiveRecord::Acts::Taggable) + +require 'puppet/rails/external/tagging/tagging' +require 'puppet/rails/external/tagging/tag' diff --git a/lib/puppet/rails/external/tagging/tag.rb b/lib/puppet/rails/external/tagging/tag.rb new file mode 100644 index 000000000..191abb08c --- /dev/null +++ b/lib/puppet/rails/external/tagging/tag.rb @@ -0,0 +1,50 @@ +class Tag < ActiveRecord::Base + has_many :taggings + + def self.tags(options = {}) + query = "select tags.id, name, count(*) as count" + query << " from taggings, tags" + query << " where tags.id = tag_id" + query << " group by tag_id" + query << " order by #{options[:order]}" if options[:order] != nil + query << " limit #{options[:limit]}" if options[:limit] != nil + tags = Tag.find_by_sql(query) + end + + def self.parse(list) + tag_names = [] + + # first, pull out the quoted tags + list.gsub!(/\"(.*?)\"\s*/ ) { tag_names << $1; "" } + + # then, replace all commas with a space + list.gsub!(/,/, " ") + + # then, get whatever's left + tag_names.concat list.split(/\s/) + + # strip whitespace from the names + tag_names = tag_names.map { |t| t.strip } + + # delete any blank tag names + tag_names = tag_names.delete_if { |t| t.empty? } + + return tag_names + end + + def tagged + @tagged ||= taggings.collect { |tagging| tagging.taggable } + end + + def on(taggable) + taggings.create :taggable => taggable + end + + def ==(comparison_object) + super || name == comparison_object.to_s + end + + def to_s + name + end +end diff --git a/lib/puppet/rails/external/tagging/tagging.rb b/lib/puppet/rails/external/tagging/tagging.rb new file mode 100644 index 000000000..e06e88a14 --- /dev/null +++ b/lib/puppet/rails/external/tagging/tagging.rb @@ -0,0 +1,12 @@ +class Tagging < ActiveRecord::Base + belongs_to :tag + belongs_to :taggable, :polymorphic => true + + def self.tagged_class(taggable) + ActiveRecord::Base.send(:class_name_of_active_record_descendant, taggable.class).to_s + end + + def self.find_taggable(tagged_class, tagged_id) + tagged_class.constantize.find(tagged_id) + end +end \ No newline at end of file diff --git a/lib/puppet/rails/lib/README b/lib/puppet/rails/lib/README deleted file mode 100644 index 8d2f90822..000000000 --- a/lib/puppet/rails/lib/README +++ /dev/null @@ -1,4 +0,0 @@ -Acts As Taggable -================= - -Allows for tags to be added to multiple classes. \ No newline at end of file diff --git a/lib/puppet/rails/lib/acts_as_taggable.rb b/lib/puppet/rails/lib/acts_as_taggable.rb deleted file mode 100644 index 137a1c2a8..000000000 --- a/lib/puppet/rails/lib/acts_as_taggable.rb +++ /dev/null @@ -1,62 +0,0 @@ -module ActiveRecord - module Acts #:nodoc: - module Taggable #:nodoc: - def self.included(base) - base.extend(ClassMethods) - end - - module ClassMethods - def acts_as_taggable(options = {}) - write_inheritable_attribute(:acts_as_taggable_options, { - :taggable_type => ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s, - :from => options[:from] - }) - - class_inheritable_reader :acts_as_taggable_options - - has_many :taggings, :as => :taggable, :dependent => true - has_many :tags, :through => :taggings - - include ActiveRecord::Acts::Taggable::InstanceMethods - extend ActiveRecord::Acts::Taggable::SingletonMethods - end - end - - module SingletonMethods - def find_tagged_with(list) - find_by_sql([ - "SELECT #{table_name}.* FROM #{table_name}, tags, taggings " + - "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " + - "AND taggings.taggable_type = ? " + - "AND taggings.tag_id = tags.id AND tags.name IN (?)", - acts_as_taggable_options[:taggable_type], list - ]) - end - def tags(options = {}) - options.merge!(:taggable_type => self.to_s) - Tag.tags(options) - end - end - - module InstanceMethods - def tag_with(list) - Tag.transaction do - taggings.destroy_all - - Tag.parse(list).each do |name| - if acts_as_taggable_options[:from] - send(acts_as_taggable_options[:from]).tags.find_or_create_by_name(name).on(self) - else - Tag.find_or_create_by_name(name).on(self) - end - end - end - end - - def tag_list - tags.collect { |tag| tag.name.include?(" ") ? "'#{tag.name}'" : tag.name }.join(" ") - end - end - end - end -end diff --git a/lib/puppet/rails/lib/init.rb b/lib/puppet/rails/lib/init.rb deleted file mode 100644 index 0a07e5c8e..000000000 --- a/lib/puppet/rails/lib/init.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/rails/lib/acts_as_taggable' -ActiveRecord::Base.send(:include, ActiveRecord::Acts::Taggable) - -require 'puppet/rails/lib/tagging' -require 'puppet/rails/lib/tag' diff --git a/lib/puppet/rails/lib/tag.rb b/lib/puppet/rails/lib/tag.rb deleted file mode 100644 index 191abb08c..000000000 --- a/lib/puppet/rails/lib/tag.rb +++ /dev/null @@ -1,50 +0,0 @@ -class Tag < ActiveRecord::Base - has_many :taggings - - def self.tags(options = {}) - query = "select tags.id, name, count(*) as count" - query << " from taggings, tags" - query << " where tags.id = tag_id" - query << " group by tag_id" - query << " order by #{options[:order]}" if options[:order] != nil - query << " limit #{options[:limit]}" if options[:limit] != nil - tags = Tag.find_by_sql(query) - end - - def self.parse(list) - tag_names = [] - - # first, pull out the quoted tags - list.gsub!(/\"(.*?)\"\s*/ ) { tag_names << $1; "" } - - # then, replace all commas with a space - list.gsub!(/,/, " ") - - # then, get whatever's left - tag_names.concat list.split(/\s/) - - # strip whitespace from the names - tag_names = tag_names.map { |t| t.strip } - - # delete any blank tag names - tag_names = tag_names.delete_if { |t| t.empty? } - - return tag_names - end - - def tagged - @tagged ||= taggings.collect { |tagging| tagging.taggable } - end - - def on(taggable) - taggings.create :taggable => taggable - end - - def ==(comparison_object) - super || name == comparison_object.to_s - end - - def to_s - name - end -end diff --git a/lib/puppet/rails/lib/tagging.rb b/lib/puppet/rails/lib/tagging.rb deleted file mode 100644 index e06e88a14..000000000 --- a/lib/puppet/rails/lib/tagging.rb +++ /dev/null @@ -1,12 +0,0 @@ -class Tagging < ActiveRecord::Base - belongs_to :tag - belongs_to :taggable, :polymorphic => true - - def self.tagged_class(taggable) - ActiveRecord::Base.send(:class_name_of_active_record_descendant, taggable.class).to_s - end - - def self.find_taggable(tagged_class, tagged_id) - tagged_class.constantize.find(tagged_id) - end -end \ No newline at end of file diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb index 43d7bbf4b..2608daef1 100644 --- a/lib/puppet/rails/resource.rb +++ b/lib/puppet/rails/resource.rb @@ -1,5 +1,5 @@ require 'puppet' -require 'puppet/rails/lib/init' +require 'puppet/rails/external/tagging/init' require 'puppet/rails/param_name' require 'puppet/util/rails/collection_merger' -- cgit