diff options
| author | hukl <hukl@eight.local> | 2009-02-02 23:46:46 +0100 |
|---|---|---|
| committer | hukl <hukl@eight.local> | 2009-02-02 23:46:46 +0100 |
| commit | 1bbeefe805efba28ef9c237a1c27f3f1ce1d5dc0 (patch) | |
| tree | af9103e8e23b097a930f6fa6930dca470d657043 | |
| parent | 1c8bcc58d410db6d7eb5f1629813f08f78f47fa1 (diff) | |
refactored the whole tagging thing. now using
acts_as_taggable_on_steroids instead of a
homebrew solution
| -rw-r--r-- | app/helpers/content_helper.rb | 4 | ||||
| -rw-r--r-- | app/models/flag.rb | 3 | ||||
| -rw-r--r-- | app/models/page.rb | 41 | ||||
| -rw-r--r-- | db/migrate/20090201211159_create_flags.rb | 13 | ||||
| -rw-r--r-- | lib/update_importer.rb | 21 | ||||
| -rw-r--r-- | test/fixtures/flags.yml | 7 | ||||
| -rw-r--r-- | test/unit/flag_test.rb | 8 |
7 files changed, 12 insertions, 85 deletions
diff --git a/app/helpers/content_helper.rb b/app/helpers/content_helper.rb index 3cf3488..385bca0 100644 --- a/app/helpers/content_helper.rb +++ b/app/helpers/content_helper.rb | |||
| @@ -10,7 +10,7 @@ module ContentHelper | |||
| 10 | begin | 10 | begin |
| 11 | if content =~ /<aggregate([^<>]*)>/ | 11 | if content =~ /<aggregate([^<>]*)>/ |
| 12 | tag = $~.to_s | 12 | tag = $~.to_s |
| 13 | matched_data = $1.scan(/\w+\=\"[a-zA-Z\s\/_\d]*\"/) | 13 | matched_data = $1.scan(/\w+\=\"[a-zA-Z\s\/_\d,]*\"/) |
| 14 | 14 | ||
| 15 | matched_data.each do |data| | 15 | matched_data.each do |data| |
| 16 | splitted_data = data.split("=") | 16 | splitted_data = data.split("=") |
| @@ -18,6 +18,8 @@ module ContentHelper | |||
| 18 | end | 18 | end |
| 19 | 19 | ||
| 20 | content.sub(tag, render_collection(options)) | 20 | content.sub(tag, render_collection(options)) |
| 21 | else | ||
| 22 | content | ||
| 21 | end | 23 | end |
| 22 | 24 | ||
| 23 | rescue | 25 | rescue |
diff --git a/app/models/flag.rb b/app/models/flag.rb deleted file mode 100644 index 6d67377..0000000 --- a/app/models/flag.rb +++ /dev/null | |||
| @@ -1,3 +0,0 @@ | |||
| 1 | class Flag < ActiveRecord::Base | ||
| 2 | has_and_belongs_to_many :pages | ||
| 3 | end | ||
diff --git a/app/models/page.rb b/app/models/page.rb index e1bddb2..5908029 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -1,39 +1,9 @@ | |||
| 1 | # Alternativ queries for the named_scope with one or two inner joins. | ||
| 2 | # Loading the Flags themselves would be another query. | ||
| 3 | # Could be faster on larger data sets. | ||
| 4 | # | ||
| 5 | # Single Join: | ||
| 6 | # | ||
| 7 | # Page.find( | ||
| 8 | # :all, | ||
| 9 | # :joins => 'JOIN flags_pages on pages.id = flags_pages.page_id', | ||
| 10 | # :include => :flags, | ||
| 11 | # :conditions => ['flags_pages.flag_id IN (?)', [1,2]] | ||
| 12 | # ) | ||
| 13 | # Two inner joins: | ||
| 14 | # | ||
| 15 | # Page.find( | ||
| 16 | # :all, | ||
| 17 | # :joins => :flags_pages, | ||
| 18 | # :conditions => ['flags_pages.flag_id IN (?)', [1,2]] | ||
| 19 | # ) | ||
| 20 | # | ||
| 21 | # Page.find_by_sql("select p.* from pages p JOIN flags_pages f on p.id = f.page_id where (f.flag_id IN (1,2))") | ||
| 22 | |||
| 23 | class Page < ActiveRecord::Base | 1 | class Page < ActiveRecord::Base |
| 24 | 2 | ||
| 25 | belongs_to :node | 3 | belongs_to :node |
| 26 | has_and_belongs_to_many :flags | ||
| 27 | |||
| 28 | acts_as_list :column => :revision, :scope => :node_id | ||
| 29 | 4 | ||
| 30 | named_scope :with_flags, lambda {|flag_names| | 5 | acts_as_taggable |
| 31 | if (flags = Flag.find_all_by_name(flag_names)).empty? | 6 | acts_as_list :column => :revision, :scope => :node_id |
| 32 | {} | ||
| 33 | else | ||
| 34 | {:include => :flags, :conditions => ['flags_pages.flag_id IN (?)', flags.map(&:id)] } | ||
| 35 | end | ||
| 36 | } | ||
| 37 | 7 | ||
| 38 | # <aggregate | 8 | # <aggregate |
| 39 | # flags="updates pressemitteilungen" | 9 | # flags="updates pressemitteilungen" |
| @@ -51,10 +21,7 @@ class Page < ActiveRecord::Base | |||
| 51 | } | 21 | } |
| 52 | 22 | ||
| 53 | options = defaults.merge options | 23 | options = defaults.merge options |
| 54 | 24 | debugger | |
| 55 | pages = Page.with_flags(options[:flags].split(/\s/)).all( | 25 | pages = Page.find_tagged_with(options[:flags], :match_all => true) |
| 56 | :limit => options[:limit], | ||
| 57 | :order => "#{options[:order_by]} #{options[:order_direction]}" | ||
| 58 | ) | ||
| 59 | end | 26 | end |
| 60 | end \ No newline at end of file | 27 | end \ No newline at end of file |
diff --git a/db/migrate/20090201211159_create_flags.rb b/db/migrate/20090201211159_create_flags.rb deleted file mode 100644 index 3b9da43..0000000 --- a/db/migrate/20090201211159_create_flags.rb +++ /dev/null | |||
| @@ -1,13 +0,0 @@ | |||
| 1 | class CreateFlags < ActiveRecord::Migration | ||
| 2 | def self.up | ||
| 3 | create_table :flags do |t| | ||
| 4 | t.string :name | ||
| 5 | |||
| 6 | t.timestamps | ||
| 7 | end | ||
| 8 | end | ||
| 9 | |||
| 10 | def self.down | ||
| 11 | drop_table :flags | ||
| 12 | end | ||
| 13 | end | ||
diff --git a/lib/update_importer.rb b/lib/update_importer.rb index 7947376..ec9033a 100644 --- a/lib/update_importer.rb +++ b/lib/update_importer.rb | |||
| @@ -17,18 +17,7 @@ class UpdateImporter | |||
| 17 | @updates = Node.create!( :slug => 'updates' ) | 17 | @updates = Node.create!( :slug => 'updates' ) |
| 18 | @updates.move_to_child_of Node.root | 18 | @updates.move_to_child_of Node.root |
| 19 | end | 19 | end |
| 20 | 20 | ||
| 21 | unless @update_flag = Flag.find_by_name("update") | ||
| 22 | @update_flag = Flag.create!( :name => "update" ) | ||
| 23 | end | ||
| 24 | |||
| 25 | unless @pm_flag = Flag.find_by_name("pressemitteilung") | ||
| 26 | @pm_flag = Flag.create!( :name => "pressemitteilung" ) | ||
| 27 | end | ||
| 28 | |||
| 29 | unless @event_flag = Flag.find_by_name("event") | ||
| 30 | @event_flag = Flag.create!( :name => "event" ) | ||
| 31 | end | ||
| 32 | end | 21 | end |
| 33 | 22 | ||
| 34 | # Class Methods | 23 | # Class Methods |
| @@ -96,12 +85,12 @@ class UpdateImporter | |||
| 96 | ) | 85 | ) |
| 97 | end | 86 | end |
| 98 | 87 | ||
| 99 | page.flags << @update_flag if page | 88 | page.tag_list.add("update") if page |
| 100 | 89 | ||
| 101 | if (flags = xhtml.elements['flags']) && page | 90 | if (flags = xhtml.elements['flags']) && page |
| 102 | page.flags << @event_flag if flags.attributes['calendar'] | 91 | page.tag_list.add("event") if flags.attributes['calendar'] |
| 103 | page.flags << @pm_flag if flags.attributes['pm'] | 92 | page.tag_list.add("pressemitteilung") if flags.attributes['pm'] |
| 104 | 93 | page.save | |
| 105 | print "#{page.title} >>> " | 94 | print "#{page.title} >>> " |
| 106 | puts flags.attributes['calendar'].inspect | 95 | puts flags.attributes['calendar'].inspect |
| 107 | end | 96 | end |
diff --git a/test/fixtures/flags.yml b/test/fixtures/flags.yml deleted file mode 100644 index 157d747..0000000 --- a/test/fixtures/flags.yml +++ /dev/null | |||
| @@ -1,7 +0,0 @@ | |||
| 1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html | ||
| 2 | |||
| 3 | one: | ||
| 4 | name: MyString | ||
| 5 | |||
| 6 | two: | ||
| 7 | name: MyString | ||
diff --git a/test/unit/flag_test.rb b/test/unit/flag_test.rb deleted file mode 100644 index 49b0d96..0000000 --- a/test/unit/flag_test.rb +++ /dev/null | |||
| @@ -1,8 +0,0 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class FlagTest < ActiveSupport::TestCase | ||
| 4 | # Replace this with your real tests. | ||
| 5 | test "the truth" do | ||
| 6 | assert true | ||
| 7 | end | ||
| 8 | end | ||
