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 +++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 app/models/flag.rb (limited to 'app/models') 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 -- cgit v1.3