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

232 lines
5.8 KiB
Ruby
Raw Normal View History

2011-03-31 00:31:39 +00:00
require 'active_record'
case ENV['DB'].try(:downcase)
when 'mysql', 'mysql2'
# To test with MySQL: `DB=mysql bundle exec rake spec`
ActiveRecord::Base.establish_connection(
adapter: 'mysql2',
database: 'ransack',
encoding: 'utf8'
)
when 'pg', 'postgres', 'postgresql'
# To test with PostgreSQL: `DB=postgresql bundle exec rake spec`
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: 'ransack',
# username: 'postgres', # Uncomment the username option if you have set one
min_messages: 'warning'
)
else
# Otherwise, assume SQLite3: `bundle exec rake spec`
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)
end
2011-03-31 00:31:39 +00:00
class Person < ActiveRecord::Base
if ActiveRecord::VERSION::MAJOR == 3
default_scope order('id DESC')
else
default_scope { order(id: :desc) }
end
2015-11-18 22:51:23 +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
if ActiveRecord::VERSION::MAJOR == 3
has_many :published_articles, conditions: { published: true }, class_name: "Article"
else
has_many :published_articles, ->{ where(published: true) }, class_name: "Article"
end
2011-03-31 00:31:39 +00:00
has_many :comments
2015-11-18 22:51:23 +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
scope :restricted, lambda { where("restricted = 1") }
scope :active, lambda { where("active = 1") }
scope :over_age, lambda { |y| where(["age > ?", y]) }
2015-01-14 17:43:41 +00:00
scope :of_age, lambda { |of_age|
of_age ? where("age >= ?", 18) : where("age < ?", 18)
}
2015-09-23 14:03:04 +00:00
alias_attribute :full_name, :name
2015-11-18 22:51:23 +00:00
ransacker :reversed_name, formatter: proc { |v| v.reverse } do |parent|
2011-04-11 16:59:26 +00:00
parent.table[:name]
end
2015-01-14 17:43:41 +00:00
ransacker :array_users,
formatter: proc { |v| Person.first(2).map(&:id) } do |parent|
parent.table[:id]
end
2015-01-14 17:43:41 +00:00
ransacker :array_names,
formatter: proc { |v| Person.first(2).map { |p| p.id.to_s } } do |parent|
parent.table[:name]
end
ransacker :doubled_name do |parent|
2013-12-07 00:51:55 +00:00
Arel::Nodes::InfixOperation.new(
'||', parent.table[:name], parent.table[:name]
)
end
ransacker :sql_literal_id do
Arel.sql('people.id')
end
ransacker :with_arguments, args: [:parent, :ransacker_args] do |parent, args|
min, max = args
query = <<-SQL
(SELECT MAX(articles.title)
2015-02-17 16:31:38 +00:00
FROM articles
WHERE articles.person_id = people.id
AND LENGTH(articles.body) BETWEEN #{min} AND #{max}
GROUP BY articles.person_id
2015-02-17 16:31:38 +00:00
)
SQL
Arel.sql(query)
2015-02-17 16:31:38 +00:00
end
2013-12-07 00:51:55 +00:00
def self.ransackable_attributes(auth_object = nil)
if auth_object == :admin
2015-09-23 14:03:04 +00:00
super - ['only_sort']
2013-12-07 00:51:55 +00:00
else
2015-09-23 14:03:04 +00:00
super - ['only_sort', 'only_admin']
2013-12-07 00:51:55 +00:00
end
end
def self.ransortable_attributes(auth_object = nil)
if auth_object == :admin
column_names + _ransackers.keys - ['only_search']
else
column_names + _ransackers.keys - ['only_search', 'only_admin']
end
end
2011-03-31 00:31:39 +00:00
end
class Article < ActiveRecord::Base
belongs_to :person
has_many :comments
2011-03-31 00:31:39 +00:00
has_and_belongs_to_many :tags
2015-11-18 22:51:23 +00:00
has_many :notes, as: :notable
if ActiveRecord::VERSION::STRING >= '3.1'
default_scope { where("'default_scope' = 'default_scope'") }
else # Rails 3.0 does not accept a block
default_scope where("'default_scope' = 'default_scope'")
end
2011-03-31 00:31:39 +00:00
end
class Recommendation < ActiveRecord::Base
belongs_to :person
belongs_to :target_person, class_name: 'Person'
belongs_to :article
end
module Namespace
class Article < ::Article
end
end
module Namespace
class Article < ::Article
end
end
2011-03-31 00:31:39 +00:00
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
2015-11-18 22:51:23 +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
2015-11-18 22:51:23 +00:00
create_table :people, force: true do |t|
t.integer :parent_id
t.string :name
t.string :email
2013-12-07 00:51:55 +00:00
t.string :only_search
t.string :only_sort
t.string :only_admin
t.string :new_start
t.string :stop_end
t.integer :salary
t.date :life_start
2013-08-04 13:13:41 +00:00
t.boolean :awesome, default: false
t.boolean :terms_and_conditions, default: false
t.boolean :true_or_false, default: true
2014-09-23 12:06:43 +00:00
t.timestamps null: false
end
2011-03-31 00:31:39 +00:00
2015-11-18 22:51:23 +00:00
create_table :articles, force: true do |t|
t.integer :person_id
t.string :title
t.text :subject_header
t.text :body
t.boolean :published, default: true
end
2011-03-31 00:31:39 +00:00
2015-11-18 22:51:23 +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
2015-11-18 22:51:23 +00:00
create_table :tags, force: true do |t|
t.string :name
end
2011-03-31 00:31:39 +00:00
2015-11-18 22:51:23 +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
2015-11-18 22:51:23 +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
2015-11-18 22:51:23 +00:00
create_table :recommendations, force: true do |t|
t.integer :person_id
t.integer :target_person_id
t.integer :article_id
end
2011-03-31 00:31:39 +00:00
end
10.times do
person = Person.make
2015-11-18 22:51:23 +00:00
Note.make(notable: person)
2011-03-31 00:31:39 +00:00
3.times do
2015-11-18 22:51:23 +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
2015-11-18 22:51:23 +00:00
Note.make(notable: article)
2011-03-31 00:31:39 +00:00
10.times do
2015-11-18 22:51:23 +00:00
Comment.make(article: article, person: person)
2011-03-31 00:31:39 +00:00
end
end
end
2013-12-07 00:51:55 +00:00
Comment.make(
2015-11-18 22:51:23 +00:00
body: 'First post!',
article: Article.make(title: 'Hello, world!')
)
2011-03-31 00:31:39 +00:00
end
end