Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed scope out and unused parent attachments

  • Loading branch information...
commit 09809818237c547a33549028b386a74aec20f6e9 1 parent 97e1874
@codafoo codafoo authored queso committed
View
2  app/controllers/attachments_controller.rb
@@ -2,7 +2,7 @@ class AttachmentsController < ApplicationController
# GET /attachments
# GET /attachments.xml
def index
- @attachments = Attachment.find_parent(:all)
+ @attachments = Attachment.find(:all)
respond_to do |format|
format.html # index.html.erb
View
2  app/models/attachment.rb
@@ -19,8 +19,6 @@
class Attachment < ActiveRecord::Base
belongs_to :page
- scope_out :parent, :conditions => ["parent_id IS ?", nil]
-
has_attachment :storage => :file_system,
:thumbnails => { :thumb => [50, 50], :small => "100x100>", :large => "400x400>"},
:processor => :rmagick
View
106 vendor/plugins/scope_out/README
@@ -1,106 +0,0 @@
-=ScopeOut
-Author:: John Andrews
-License:: Distributes under the same terms as Ruby
-============
-
-==Usage:
-
- class Person < ActiveRecord::Base
- scope_out :women, :conditions => ["people.sex = ?", "F"]
- end
-
-The above code creates three class methods: find_women, with_women, and calculate_women.
-It is equivalent to doing the following:
-
- class Person < ActiveRecord::Base
- def Person.with_women
- with_scope :find => {:conditions => ['people.sex => ?', "F"]} do
- yield
- end
- end
-
- def Person.find_women(*args)
- with_women {find(*args)}
- end
-
- def Person.calculate_women(*args)
- with_women {calculate(*args)}
- end
- end
-
-==with_x
-Person.with_women acts just like with_scope, except the scope is already defined. For example:
-
- Person.with_women do
- Person.find(:all, :order => 'people.age desc')
- end
-
-==find_x
-Person.find_women acts just like find, except that it is scoped with with_women:
-
- Person.calculate_women(:first, :include => :pets)
- # equivalent to Person.find(:first, :conditions => ["people.sex = ?", "F"], :include => :pets)
-
-==calculate_x
-Person.calculate_active (you guessed it) is exactly like Person.calculate, but scoped with 'with_women'
-
- Person.calculate_active(:count, :all)
- # is the same as Person.calculate(:count, :all, :conditions => ["person.sex = ?", "F"])
-
-==Combined Scopes
-You can use the combined_scope method to define a scope which is the combination of two or more scopes that you previously defined with scope_out.
-
- class Ticket
- scope_out :active
- scope_out :johns_tickets, :conditions => {:assigned_to => 'John'}
- combined_scope :todo_for_john, [:active, :johns_tickets]
- end
-
-==Association Finders
-scope_out also creates a module called AssociationMethods inside the class that defines the scope. Using this, you can extend your associations using the same scopes. Let's define a second class:
-
- class Person < ActiveRecord::Base
- has_many :pets, :extend => Pet::AssociationMethods
- scope_out :women, :conditions => ["people.sex = ?", "F"]
- end
-
- class Pet < ActiveRecord::Base
- belongs_to :person
- scope_out :cats, :conditions => ["pets.type = ?", "Feline"]
- end
-
- woman = Person.find_women(:first)
- her_pets = woman.pets
- her_cats = woman.pets.cats
-
- # cats is cached on the association, so this doesn't cause another call to the database
- # unless you do woman.pets.cats(:reload)
- puts "Cat Lady!" if woman.pets.cats.length > 4
-
-==Dynamic Finders
-scope_out extends ActiveRecord::Base#method_missing to capture dynamic, scope-based finders.
-
- female_centurions = Person.find_all_women_by_age(100)
- scruffy = Pet.find_cats_by_name('Scruffy')
-
-==Flexible Syntax
-All of the following define the same scope:
-
- class Ticket
- scope_out :active
- scope_out :active, :field => 'active', :value => true
- scope_out :active, :conditions => ["active = ?", true]
- scope_out :active, :conditions => {:active => true} # rails >= 1.2
- end
-
-Note: the :field, :value syntax will be deprecated in the near future in favor of the :conditions hash.
-
-==Dynamic Conditions
-If you want to use a dynamic condition (which will be evaluated each time the scope is called), you can pass the options hash as a block.
-
- class Person < ActiveRecord::Base
- scope_out :adults do
- {:conditions => ['people.birthdate < ?', 18.years.ago],
- :order => 'people.birthdate asc' }
- end
- end
View
22 vendor/plugins/scope_out/Rakefile
@@ -1,22 +0,0 @@
-require 'rake'
-require 'rake/testtask'
-require 'rake/rdoctask'
-
-desc 'Default: run unit tests.'
-task :default => :test
-
-desc 'Test the scope_out plugin.'
-Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
-end
-
-desc 'Generate documentation for the scope_out plugin.'
-Rake::RDocTask.new(:rdoc) do |rdoc|
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'ScopeOut'
- rdoc.options << '--line-numbers' << '--inline-source'
- rdoc.rdoc_files.include('README')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
View
2  vendor/plugins/scope_out/init.rb
@@ -1,2 +0,0 @@
-require 'scope_out'
-ActiveRecord::Base.send(:include, ScopeOut)
View
1  vendor/plugins/scope_out/install.rb
@@ -1 +0,0 @@
-# Install hook code here
View
134 vendor/plugins/scope_out/lib/scope_out.rb
@@ -1,134 +0,0 @@
-module ScopeOut
- def self.included(base)
- base.extend ClassMethods
- end
-
- module ClassMethods
- ASSOC_MODULE_NAME = "AssociationMethods"
- # given a name such as 'active', scope_out creates 3 methods
- # with_active creates a scope
- # find_active uses the with_active scope to call find
- # calculate_active uses the with_active scope to call calculate
- def scope_out(name, options = {}, &block)
- @scope_out_blocks ||= {}
- if block_given?
- puts "WARNING: scope_out :options ignored if block given" unless options.empty?
- @scope_out_blocks[name] = block
- else
- opts = prepare_scope_out_options(name, options)
- end
-
- # create new class methods
- instance_eval <<-DEFINE_METHODS
- protected
- def with_#{name}
- with_scope :find => #{@scope_out_blocks[name] ? "@scope_out_blocks[#{name.inspect}].call" : opts.inspect} do
- yield
- end
- end
- DEFINE_METHODS
-
- # create find_x and calculate_x methods
- add_scoped_finders name
- # create a memoized association finder
- add_association_method name
- end
-
- # use previously defined with_scopes to define a new, composite scope and finders
- def combined_scope(name, scopes, options = nil)
- return if (scopes = Array(scopes)).blank?
- new_scope = options ? "with_scope(:find => #{options.inspect}) {yield}" : 'yield'
- scope_string = scopes.inject(new_scope) do |str, scope|
- str.sub('yield', "with_#{scope} {yield}")
- end
- instance_eval "def with_#{name}() #{scope_string}; end"
- add_scoped_finders name
- add_association_method name
- end
-
- # allow scope_out users to be able to do cool stuff like
- # Student.find_all_active_by_age(18)
- # this will use the with_active scope and then pass :find_all_by_age up the method_missing chain
- def method_missing_with_scope_out(method_called, *args, &block)
- method_str = method_called.to_s
- match = method_str.match(/^(find_)(all_)?(\w+)(_by_)(\w+)$/)
- if match && match[3] != 'all'
- # our scope name should be in $3
- scope_str = match[3]
- method_str = match.captures
- # delete scope name (match is omitted from captures)
- method_str.delete_at(2)
- method_str = method_str.compact.join.gsub('__', '_')
- scope_sym = "with_#{scope_str}".to_sym
- # call ActiveRecord method missing to do the real work,
- # after wrapping it in our scope
- send scope_sym do
- send method_str.to_sym, *args, &block
- end
- else
- # doesn't match our string. pass it on to ActiveRecord.method_missing
- method_missing_without_scope_out(method_called, *args, &block)
- end
- end
- alias_method_chain :method_missing, :scope_out
-
- private
-
- # allows scope_out to be defined in any of the following ways (all equivalent)
- # scope_out :active, :field => :active, :value => true
- # scope_out :active, :value => true
- # scope_out :active, :field => :active
- # scope_out :active
- # scope_out :active, :conditions => ["active = ?", true]
- # scope_out :active do
- # {:conditions => ["active = ?", true]}
- # end
- def prepare_scope_out_options(name, opts)
- returning opts.dup do |options|
- if options[:conditions]
- raise "Contradictory options to scope_out" if options.has_key?(:value) || options.has_key?(:field)
- else
- # field defaults to name, and value defaults to true
- options[:value] = true unless options.has_key?(:value)
- options[:conditions] ||= { (options[:field] || name).to_sym => options[:value] }
- end
- options.delete :field
- options.delete :value
- end
- end
-
- def add_association_method(name)
- # create a memoized association finder
- association_module.class_eval <<-DEF_ASSOC
- def #{name}(reload = false)
- @#{name}_memoized = nil if reload
- @#{name}_memoized ||= find_#{name}(:all)
- end
- DEF_ASSOC
- end
-
- def add_scoped_finders(name)
- instance_eval <<-DEF_FINDERS
- def find_#{name}(*args)
- with_#{name} {find(*args)}
- end
-
- def calculate_#{name}(*args)
- with_#{name} {calculate(*args)}
- end
- DEF_FINDERS
- end
-
- # create a new anonymous module inside of our class
- # store a reference to it in a constant
- def association_module(module_name = ASSOC_MODULE_NAME)
- unless const_defined?(module_name)
- class_eval do
- const_set(module_name, Module.new)
- end
- end
- const_get module_name
- end
-
- end
-end
View
4 vendor/plugins/scope_out/tasks/active_finder_tasks.rake
@@ -1,4 +0,0 @@
-# desc "Explaining what the task does"
-# task :active_finder do
-# # Task goes here
-# end
View
18 vendor/plugins/scope_out/test/database.yml
@@ -1,18 +0,0 @@
-sqlite:
- :adapter: sqlite
- :dbfile: plugin.sqlite.db
-sqlite3:
- adapter: sqlite3
- dbfile: ":memory:"
-postgresql:
- :adapter: postgresql
- :username: postgres
- :password: postgres
- :database: plugin_test
- :min_messages: ERROR
-mysql:
- :adapter: mysql
- :host: localhost
- :username:
- :password:
- :database: scope_out_test
View
22 vendor/plugins/scope_out/test/fixtures/schools.yml
@@ -1,22 +0,0 @@
-<% years = 365 %>
-harvard:
- id: 1
- name: Harvard
- ivy_league: true
- founded: <%= Date.today-100*years %>
- bool_one: true
- bool_two: true
-yale:
- id: 2
- name: Yale
- ivy_league: true
- founded: <%= Date.today-150*years %>
- bool_one: true
- bool_two: true
-ou:
- id: 3
- name: Ohio University
- ivy_league: false
- founded: <%= Date.today-200*years %>
- bool_one: false
- bool_two: false
View
55 vendor/plugins/scope_out/test/fixtures/students.yml
@@ -1,55 +0,0 @@
-jack:
- id: 1
- name: jack
- level: Freshman
- age: 18
- active: true
- gpa: 4.0
- school_id: 3
-jill:
- id: 2
- name: jill
- level: Senior
- age: 17
- active: false
- gpa: 1.2
- school_id: 2
-holly:
- id: 3
- name: holly
- level: Freshman
- age: 16
- active: false
- gpa: 3.5
- school_id: 1
-gretchen:
- id: 4
- name: gretchen
- level: Senior
- age: 18
- active: false
- gpa: 3.0
- school_id: 2
-william:
- id: 5
- name: william
- level: Senior
- age: 67
- active: true
- gpa: 2.2
- school_id: 3
-aleister:
- id: 6
- name: aleister
- level: Junior
- age: 22
- gpa: 3.9
- school_id: 3
- active: false
-anonymous:
- id: 7
- level: Senior
- age: 21
- gpa: 3.0
- school_id: 1
- active: true
View
19 vendor/plugins/scope_out/test/schema.rb
@@ -1,19 +0,0 @@
-ActiveRecord::Schema.define(:version => 1) do
-
- create_table :students do |t|
- t.column :name, :string
- t.column :level, :string
- t.column :age, :integer
- t.column :active, :boolean
- t.column :gpa, :decimal
- t.column :school_id, :integer
- end
-
- create_table :schools do |t|
- t.column :name, :string
- t.column :ivy_league, :boolean
- t.column :founded, :date
- t.column :bool_one, :boolean
- t.column :bool_two, :boolean
- end
-end
View
276 vendor/plugins/scope_out/test/scope_out_test.rb
@@ -1,276 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + "/test_helper")
-
-class DummyDate
- @@dates = [Date.today, Date.today-30*365]
- def self.today
- @@dates.shift
- end
-end
-
-class DumbScoper
- @@names = %w{Harvard Yale}
- def self.name
- @@names.shift
- end
-end
-
-class Student < ActiveRecord::Base
- scope_out :freshmen, :field => :level, :value => 'Freshman'
- scope_out :eighteen_year_olds, :field => :age, :value => 18
- scope_out :active, :field => :active, :value => true
- scope_out :inactive, :field => :active, :value => false
- scope_out :one_active, :field => :active, :value => true, :limit => 1
- scope_out :anonymous, :field => :name, :value => nil
- combined_scope :active_freshmen, [:active, :freshmen]
- scope_out :freshmen_active, :conditions => ["students.active = ? AND students.level = ?", true, 'Freshman']
- belongs_to :school
- combined_scope :sexually_active_freshmen, [:active, :freshmen], :conditions => "students.id = students.id"
- combined_scope :sexually_promiscuous, :active, :conditions => "students.id = students.id"
-end
-
-class School < ActiveRecord::Base
- has_many :students, :extend => Student::AssociationMethods
- scope_out :old do
- {:conditions => ["schools.founded < ?", DummyDate.today-125*365]}
- end
- scope_out :dumb_scope do
- {:conditions => ["schools.name = ?", DumbScoper.name]}
- end
-
- # defaults to :field => :ivy_league, :value => true
- scope_out :ivy_league
- scope_out :bool_one, :value => true
- scope_out :bool_two, :field => :bool_two
-end
-
-class Student < ActiveRecord::Base
- scope_out :old, :conditions => "students.id = students.id"
-end
-
-class ScopeOutTest < Test::Unit::TestCase
- fixtures :students, :schools
-
- def setup
- @scopes = %w{freshmen eighteen_year_olds active freshmen_active inactive one_active anonymous active_freshmen sexually_active_freshmen sexually_promiscuous}
- end
-
- def test_method_added_to_student_class
- @scopes.each do |field|
- assert Student.methods.include?("find_#{field}")
- assert Student.protected_methods.include?("with_#{field}")
- assert Student.methods.include?("calculate_#{field}")
- end
- end
-
- def test_defaults_to_boolean_true
- assert_equal( School.find(:all, :conditions => ["ivy_league = ?", true]),
- School.find_ivy_league(:all))
- end
-
- def test_value_false
- assert Student.find_inactive(:all).all? { |s| !s.active? }
- end
-
- def test_other_ways_to_define_boolean
- assert_equal(School.find_bool_one(:all), School.find_bool_two(:all))
- end
-
- def test_string_finder
- freshmen_boring = Student.find_all_by_level('Freshman')
- freshmen_awesome = Student.find_freshmen(:all)
- assert_equal(freshmen_boring.length, freshmen_awesome.length)
- freshmen_boring.each { |bore| assert(freshmen_awesome.include?(bore)) }
- freshmen_boring.each { |awesome| assert(freshmen_boring.include?(awesome)) }
-
- using_public_scopes(Student) do
- assert_equal( Student.find_all_by_level_and_age('Freshman', 16),
- Student.with_freshmen{Student.find(:all, :conditions => "age = 16")})
- end
- end
-
- def test_integer_finder
- assert_equal(Student.find_all_by_age(18), Student.find_eighteen_year_olds(:all))
- end
-
- def test_boolean_finder
- assert_equal(Student.find_all_by_active(true, :order => 'age'), Student.find_active(:all, :order => 'age'))
- end
-
- def test_combined_scopes_with_additional_options
- assert_equal Student.find_sexually_active_freshmen(:all), Student.find_active_freshmen(:all)
- end
-
- def test_single_combined_scope_with_additional_options
- assert_equal Student.find_sexually_promiscuous(:all), Student.find_active(:all)
- end
-
- def test_should_allow_nil_scope
- assert_equal(Student.find(:all, :conditions => {:name => nil}),
- Student.find_anonymous(:all))
- end
-
- def test_should_allow_include_with_same_name_fields_in_conditions
- assert_equal(Student.find(:all, :conditions => {:name => nil}, :include => :school),
- Student.find_anonymous(:all, :include => :school))
- end
-
- def test_combined_scopes
- using_public_scopes(Student) do
- Student.with_freshmen do
- Student.with_eighteen_year_olds do
- Student.with_active do
- t = Student.find :all
- assert_equal([students(:jack)], t)
- end
- end
- end
- end
- end
-
- def test_combined_scope_method
- # for the duration of this test, make protected methods public
- using_public_scopes(Student) do
- s = []
- Student.with_active do
- Student.with_freshmen do
- s = Student.find :all
- end
- end
- assert_equal(s, Student.with_active_freshmen{ Student.find :all })
- end
- end
-
- def test_combined_scope_adds_find_and_calculate
- [:with_active_freshmen, :find_active_freshmen, :calculate_active_freshmen].each { |s| assert Student.respond_to?(s) }
- end
-
- def test_combined_scope_adds_scope_to_association_methods
- assert Student::AssociationMethods.instance_methods.include?("active_freshmen")
- end
-
- def test_should_not_be_in_super_class
- assert (Student.methods - ActiveRecord::Base.methods).include?("find_" + @scopes.first)
- end
-
- def test_should_accept_explicit_conditions
- normal_find = Student.find(:all, :conditions => ["students.active = ? AND students.level = ?", true, 'Freshman'])
- using_public_scopes(Student) do
- Student.with_freshmen_active do
- assert_equal(normal_find, Student.find(:all))
- end
- end
- assert_equal(normal_find, Student.find_freshmen_active(:all))
- end
-
- def test_calculate_methods
- assert_equal(Student.calculate(:count, :all, :conditions => "level = 'Freshman'"),
- Student.calculate_freshmen(:count, :all))
- assert_equal(Student.calculate(:count, :all, :conditions => "age = 18"),
- Student.calculate_eighteen_year_olds(:count, :all))
- assert_equal(Student.calculate(:count, :all, :conditions => ["active = ?", true]),
- Student.calculate_active(:count, :all))
- assert_equal(Student.calculate(:count, :all, :conditions => ["active = ? and level = ?", true, 'Freshman']),
- Student.calculate_freshmen_active(:count, :all))
- end
-
- def test_passes_limit_option_to_with_scope
- assert_equal(Student.find_all_by_active(true, :limit => 1), Student.find_one_active(:all))
- end
-
- def test_should_define_module
- assert(defined? Student::AssociationMethods)
- end
-
- def test_should_put_finders_in_module
- @scopes.each do |scope|
- assert(Student::AssociationMethods.method_defined?(scope),
- "method '#{scope}' not defined on Student::AssociationMethods")
- end
- end
-
- def test_association_responds_to_finders
- school = schools(:yale)
- @scopes.each { |scope| assert(school.students.respond_to?(scope)) }
- end
-
- def test_school_should_find_students
- school = schools(:ou)
- assert_equal(Student.find_all_by_active_and_school_id(true, school.id), school.students.active)
- end
-
- def test_base_should_not_contain_student_methods
- @scopes.each do |scope|
- %w{with_ find_ calculate_}.each do |meth|
- assert(!ActiveRecord::Base.methods.include?(meth+scope), "ActiveRecord::Base contains #{meth+scope} method")
- end
- end
- end
-
- def test_association_memoization
- school = schools(:ou)
- students_initial = school.students.active
- students_initial_count = students_initial.length
- new_student = Student.create(:school_id => school.id, :active => true)
- stale_students = school.students.active
- assert_equal(students_initial, stale_students)
- refreshed_students = school.students.active(:reload)
- assert_equal(students_initial_count+1, refreshed_students.length)
- end
-
- def test_should_scope_out_old_schools_dynamically
- old_schools = School.find_old(:all)
- assert old_schools.include?(schools(:ou))
- assert old_schools.include?(schools(:yale))
- assert !old_schools.include?(schools(:harvard))
-
- #2nd call should return the older date, i.e. it's dynamic
- old_schools_again = School.find_old(:all)
- assert old_schools_again.include?(schools(:ou))
- assert !old_schools_again.include?(schools(:yale))
- assert !old_schools_again.include?(schools(:harvard))
-
- #should support more than one dynamic scope per model
- assert_equal schools(:harvard), School.find_dumb_scope(:first)
- assert_equal schools(:yale), School.find_dumb_scope(:first)
- end
-
- def test_block_stored_in_school_class
- assert School.instance_variables.include?('@scope_out_blocks')
- School.ancestors.each do |klass|
- next if klass == School
- assert(!klass.instance_variables.include?('@scope_out_blocks'), "@scope_out_blocks present in #{klass}")
- end
- end
-
- def test_association_methods_per_class
- assert Student::AssociationMethods.instance_methods != School::AssociationMethods.instance_methods
- end
-
- def test_method_missing_hack_does_not_mess_with_activerecord_hack
- assert_equal students(:holly), Student.find_by_name('holly')
- end
-
- def test_method_missing_hack_works_for_find_by
- assert_equal students(:jack), Student.find_active_by_name("jack")
- end
-
- def test_method_missing_hack_works_for_find_by_all
- assert_equal [students(:jack)], Student.find_all_eighteen_year_olds_by_active(true)
- end
-
- def test_method_missing_should_still_raise_method_missing_where_appropriate
- assert_raises(NoMethodError){Student.find_non_existant_scope_by_id(1)}
- end
-
- def using_public_scopes(klass)
- raise "must pass a block" unless block_given?
- $with_methods = (klass.methods-['with_scope']).grep(/^with_.*$/)
- class << klass
- public *$with_methods
- end
- yield
- class << klass
- protected *$with_methods
- end
- end
-end
View
41 vendor/plugins/scope_out/test/test_helper.rb
@@ -1,41 +0,0 @@
-# Include this file in your test by copying the following line to your test:
-# require File.expand_path(File.dirname(__FILE__) + "/test_helper")
-
-$:.unshift(File.dirname(__FILE__) + '/../lib')
-RAILS_ROOT = File.dirname(__FILE__)
-
-require 'rubygems'
-require 'test/unit'
-require 'active_record'
-require 'active_record/fixtures'
-require 'active_support/binding_of_caller'
-require 'active_support/breakpoint'
-
-ActiveRecord::Base.configurations['test'] = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
-ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
-ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'][ENV['DB'] || 'sqlite3'])
-
-require "scope_out"
-require "#{File.dirname(__FILE__)}/../init"
-load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
-
-Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
-$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
-
-class Test::Unit::TestCase #:nodoc:
- def create_fixtures(*table_names)
- if block_given?
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
- else
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
- end
- end
-
- # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
- self.use_transactional_fixtures = true
-
- # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
- self.use_instantiated_fixtures = false
-
- # Add more helper methods to be used by all tests here...
-end
Please sign in to comment.
Something went wrong with that request. Please try again.