From 7a5555b1ac542e5fd5680cefaba3574677ecaa46 Mon Sep 17 00:00:00 2001 From: hukl Date: Mon, 2 Feb 2009 00:22:54 +0100 Subject: aggregation spike with flags associated to pages Page has now a named_scope :with_flags which accepts an array of flag names and returns corresponding pages. Can be chained with order and limit --- app/models/flag.rb | 3 +++ app/models/page.rb | 22 +++++++++++----------- db/migrate/20090201211159_create_flags.rb | 13 +++++++++++++ ...0090201211523_add_join_table_for_flags_pages.rb | 15 +++++++++++++++ test/fixtures/flags.yml | 7 +++++++ test/unit/flag_test.rb | 8 ++++++++ 6 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 app/models/flag.rb create mode 100644 db/migrate/20090201211159_create_flags.rb create mode 100644 db/migrate/20090201211523_add_join_table_for_flags_pages.rb create mode 100644 test/fixtures/flags.yml create mode 100644 test/unit/flag_test.rb diff --git a/app/models/flag.rb b/app/models/flag.rb new file mode 100644 index 0000000..6d67377 --- /dev/null +++ b/app/models/flag.rb @@ -0,0 +1,3 @@ +class Flag < ActiveRecord::Base + has_and_belongs_to_many :pages +end diff --git a/app/models/page.rb b/app/models/page.rb index 3d02e9f..d5d888f 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -1,9 +1,19 @@ class Page < ActiveRecord::Base belongs_to :node + has_and_belongs_to_many :flags acts_as_list :column => :revision, :scope => :node_id + named_scope :with_flags, lambda {|flag_names| + if (flags = Flag.find_all_by_name(flag_names)).empty? + {} + else + {:include => :flags, :conditions => ['flags_pages.flag_id IN (?)', flags.map(&:id)] } + end + } + + # "#{options[:order_by]} #{options[:order_direction]}" ) end -end - - -named_scope :flagged_as, lambda { |flags| - conditions = {} - flags.each do |flag| - conditions[flag] = true - end - - { :conditions => conditions } -} \ No newline at end of file +end \ No newline at end of file diff --git a/db/migrate/20090201211159_create_flags.rb b/db/migrate/20090201211159_create_flags.rb new file mode 100644 index 0000000..3b9da43 --- /dev/null +++ b/db/migrate/20090201211159_create_flags.rb @@ -0,0 +1,13 @@ +class CreateFlags < ActiveRecord::Migration + def self.up + create_table :flags do |t| + t.string :name + + t.timestamps + end + end + + def self.down + drop_table :flags + end +end diff --git a/db/migrate/20090201211523_add_join_table_for_flags_pages.rb b/db/migrate/20090201211523_add_join_table_for_flags_pages.rb new file mode 100644 index 0000000..99eb9c9 --- /dev/null +++ b/db/migrate/20090201211523_add_join_table_for_flags_pages.rb @@ -0,0 +1,15 @@ +class AddJoinTableForFlagsPages < ActiveRecord::Migration + def self.up + create_table :flags_pages, :id => false do |t| + t.integer :flag_id + t.integer :page_id + end + add_index :flags_pages, [:flag_id] + add_index :flags_pages, [:page_id] + end + + def self.down + remove_table :flags_pages + end + +end diff --git a/test/fixtures/flags.yml b/test/fixtures/flags.yml new file mode 100644 index 0000000..157d747 --- /dev/null +++ b/test/fixtures/flags.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/unit/flag_test.rb b/test/unit/flag_test.rb new file mode 100644 index 0000000..49b0d96 --- /dev/null +++ b/test/unit/flag_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class FlagTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end -- cgit v1.3