activerecord-hackery--ransack/spec/support/schema.rb

110 lines
2.6 KiB
Ruby
Raw Normal View History

2011-03-31 00:31:39 +00:00
require 'active_record'
ActiveRecord::Base.establish_connection(
2013-08-04 13:13:41 +00:00
adapter: 'sqlite3',
database: ':memory:'
2011-03-31 00:31:39 +00:00
)
class Person < ActiveRecord::Base
default_scope { order('id DESC') }
2013-08-04 13:13:41 +00:00
belongs_to :parent, class_name: 'Person', foreign_key: :parent_id
has_many :children, class_name: 'Person', foreign_key: :parent_id
2011-03-31 00:31:39 +00:00
has_many :articles
has_many :comments
2013-08-04 13:13:41 +00:00
has_many :authored_article_comments, through: :articles,
source: :comments, foreign_key: :person_id
has_many :notes, as: :notable
2011-04-11 16:59:26 +00:00
2013-08-04 13:13:41 +00:00
ransacker :reversed_name, formatter: proc { |v| v.reverse } do |parent|
2011-04-11 16:59:26 +00:00
parent.table[:name]
end
ransacker :doubled_name do |parent|
Arel::Nodes::InfixOperation.new('||', parent.table[:name], parent.table[:name])
end
2011-03-31 00:31:39 +00:00
end
class Article < ActiveRecord::Base
belongs_to :person
has_many :comments
has_and_belongs_to_many :tags
2013-08-04 13:13:41 +00:00
has_many :notes, as: :notable
2011-03-31 00:31:39 +00:00
end
class Comment < ActiveRecord::Base
belongs_to :article
belongs_to :person
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :articles
end
class Note < ActiveRecord::Base
2013-08-04 13:13:41 +00:00
belongs_to :notable, polymorphic: true
2011-03-31 00:31:39 +00:00
end
module Schema
def self.create
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
2013-08-04 13:13:41 +00:00
create_table :people, force: true do |t|
t.integer :parent_id
t.string :name
t.integer :salary
2013-08-04 13:13:41 +00:00
t.boolean :awesome, default: false
t.timestamps
end
2011-03-31 00:31:39 +00:00
2013-08-04 13:13:41 +00:00
create_table :articles, force: true do |t|
t.integer :person_id
t.string :title
t.text :body
end
2011-03-31 00:31:39 +00:00
2013-08-04 13:13:41 +00:00
create_table :comments, force: true do |t|
t.integer :article_id
t.integer :person_id
t.text :body
end
2011-03-31 00:31:39 +00:00
2013-08-04 13:13:41 +00:00
create_table :tags, force: true do |t|
t.string :name
end
2011-03-31 00:31:39 +00:00
2013-08-04 13:13:41 +00:00
create_table :articles_tags, force: true, id: false do |t|
t.integer :article_id
t.integer :tag_id
end
2011-03-31 00:31:39 +00:00
2013-08-04 13:13:41 +00:00
create_table :notes, force: true do |t|
t.integer :notable_id
t.string :notable_type
t.string :note
2011-03-31 00:31:39 +00:00
end
end
10.times do
person = Person.make
2013-08-04 13:13:41 +00:00
Note.make(notable: person)
2011-03-31 00:31:39 +00:00
3.times do
2013-08-04 13:13:41 +00:00
article = Article.make(person: person)
2011-03-31 00:31:39 +00:00
3.times do
article.tags = [Tag.make, Tag.make, Tag.make]
end
2013-08-04 13:13:41 +00:00
Note.make(notable: article)
2011-03-31 00:31:39 +00:00
10.times do
2013-08-04 13:13:41 +00:00
Comment.make(article: article)
2011-03-31 00:31:39 +00:00
end
end
2.times do
2013-08-04 13:13:41 +00:00
Comment.make(person: person)
2011-03-31 00:31:39 +00:00
end
end
2013-08-04 13:13:41 +00:00
Comment.make(body: 'First post!',
article: Article.make(title: 'Hello, world!'))
2011-03-31 00:31:39 +00:00
end
end