2011-03-31 00:31:39 +00:00
|
|
|
require 'active_record'
|
|
|
|
|
2015-03-25 10:06:59 +00:00
|
|
|
case ENV['DB'].try(:downcase)
|
2015-03-25 06:09:11 +00:00
|
|
|
when 'mysql', 'mysql2'
|
|
|
|
# To test with MySQL: `DB=mysql bundle exec rake spec`
|
2013-11-06 04:58:30 +00:00
|
|
|
ActiveRecord::Base.establish_connection(
|
|
|
|
adapter: 'mysql2',
|
|
|
|
database: 'ransack',
|
|
|
|
encoding: 'utf8'
|
|
|
|
)
|
2015-03-25 06:09:11 +00:00
|
|
|
when 'pg', 'postgres', 'postgresql'
|
|
|
|
# To test with PostgreSQL: `DB=postgresql bundle exec rake spec`
|
2013-11-06 04:58:30 +00:00
|
|
|
ActiveRecord::Base.establish_connection(
|
|
|
|
adapter: 'postgresql',
|
|
|
|
database: 'ransack',
|
2015-03-25 06:09:11 +00:00
|
|
|
# username: 'postgres', # Uncomment the username option if you have set one
|
2013-11-06 04:58:30 +00:00
|
|
|
min_messages: 'warning'
|
|
|
|
)
|
|
|
|
else
|
2015-03-25 06:09:11 +00:00
|
|
|
# Otherwise, assume SQLite3: `bundle exec rake spec`
|
2013-11-06 04:58:30 +00:00
|
|
|
ActiveRecord::Base.establish_connection(
|
|
|
|
adapter: 'sqlite3',
|
|
|
|
database: ':memory:'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-03-31 00:31:39 +00:00
|
|
|
class Person < ActiveRecord::Base
|
2015-12-26 07:10:24 +00:00
|
|
|
if ::ActiveRecord::VERSION::MAJOR == 3
|
2013-08-05 22:01:52 +00:00
|
|
|
default_scope order('id DESC')
|
|
|
|
else
|
2014-04-10 03:28:29 +00:00
|
|
|
default_scope { order(id: :desc) }
|
2013-08-05 22:01:52 +00:00
|
|
|
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
|
2015-07-10 20:24:19 +00:00
|
|
|
if ActiveRecord::VERSION::MAJOR == 3
|
2015-12-26 07:04:45 +00:00
|
|
|
if RUBY_VERSION >= '2.3'
|
2015-12-26 07:07:33 +00:00
|
|
|
has_many :published_articles, class_name: "Article",
|
|
|
|
conditions: "published = 't'"
|
2015-12-26 07:04:45 +00:00
|
|
|
else
|
2015-12-26 07:07:33 +00:00
|
|
|
has_many :published_articles, class_name: "Article",
|
|
|
|
conditions: { published: true }
|
2015-12-26 07:04:45 +00:00
|
|
|
end
|
2015-07-10 20:24:19 +00:00
|
|
|
else
|
2015-12-26 07:07:33 +00:00
|
|
|
has_many :published_articles, ->{ where(published: true) },
|
|
|
|
class_name: "Article"
|
2015-07-10 20:24:19 +00:00
|
|
|
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
|
|
|
|
2014-06-22 09:15:10 +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)
|
|
|
|
}
|
2014-06-22 09:15:10 +00:00
|
|
|
|
2015-09-23 14:03:04 +00:00
|
|
|
alias_attribute :full_name, :name
|
|
|
|
|
2015-12-09 04:03:07 +00:00
|
|
|
ransack_alias :term, :name_or_email
|
|
|
|
ransack_alias :daddy, :parent_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
|
2011-04-11 18:39:40 +00:00
|
|
|
|
2016-07-23 12:27:31 +00:00
|
|
|
ransacker :array_people_ids,
|
|
|
|
formatter: proc { |v| Person.first(2).map(&:id) } do |parent|
|
|
|
|
parent.table[:id]
|
|
|
|
end
|
|
|
|
|
|
|
|
ransacker :array_where_people_ids,
|
2016-07-01 07:06:44 +00:00
|
|
|
formatter: proc { |v| Person.where(id: v).map(&:id) } do |parent|
|
2015-01-03 22:53:55 +00:00
|
|
|
parent.table[:id]
|
|
|
|
end
|
|
|
|
|
2016-07-23 12:27:31 +00:00
|
|
|
ransacker :array_people_names,
|
2015-01-14 17:43:41 +00:00
|
|
|
formatter: proc { |v| Person.first(2).map { |p| p.id.to_s } } do |parent|
|
2015-01-05 00:26:09 +00:00
|
|
|
parent.table[:name]
|
|
|
|
end
|
|
|
|
|
2016-07-23 12:27:31 +00:00
|
|
|
ransacker :array_where_people_names,
|
|
|
|
formatter: proc { |v| Person.where(id: v).map { |p| p.id.to_s } } do |parent|
|
|
|
|
parent.table[:name]
|
|
|
|
end
|
|
|
|
|
2011-04-11 18:39:40 +00:00
|
|
|
ransacker :doubled_name do |parent|
|
2013-12-07 00:51:55 +00:00
|
|
|
Arel::Nodes::InfixOperation.new(
|
|
|
|
'||', parent.table[:name], parent.table[:name]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-09-28 09:41:35 +00:00
|
|
|
|
2015-01-30 23:33:28 +00:00
|
|
|
ransacker :sql_literal_id do
|
|
|
|
Arel.sql('people.id')
|
|
|
|
end
|
|
|
|
|
2018-01-08 20:32:22 +00:00
|
|
|
ransacker :name_case_insensitive, type: :string do
|
|
|
|
arel_table[:name].lower
|
|
|
|
end
|
|
|
|
|
2015-03-21 10:19:25 +00:00
|
|
|
ransacker :with_arguments, args: [:parent, :ransacker_args] do |parent, args|
|
|
|
|
min, max = args
|
|
|
|
query = <<-SQL
|
2015-03-15 14:29:00 +00:00
|
|
|
(SELECT MAX(articles.title)
|
2015-02-17 16:31:38 +00:00
|
|
|
FROM articles
|
|
|
|
WHERE articles.person_id = people.id
|
2015-03-21 10:19:25 +00:00
|
|
|
AND LENGTH(articles.body) BETWEEN #{min} AND #{max}
|
2015-03-15 14:29:00 +00:00
|
|
|
GROUP BY articles.person_id
|
2015-02-17 16:31:38 +00:00
|
|
|
)
|
|
|
|
SQL
|
2015-11-18 22:53:23 +00:00
|
|
|
.squish
|
2015-03-21 10:19:25 +00:00
|
|
|
Arel.sql(query)
|
2015-02-17 16:31:38 +00:00
|
|
|
end
|
|
|
|
|
2015-09-28 09:41:35 +00:00
|
|
|
|
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
|
2011-04-11 18:39:40 +00:00
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
|
2016-08-26 17:48:59 +00:00
|
|
|
class Musician < Person
|
|
|
|
end
|
|
|
|
|
2011-03-31 00:31:39 +00:00
|
|
|
class Article < ActiveRecord::Base
|
2014-05-01 13:55:39 +00:00
|
|
|
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
|
2014-09-03 09:18:42 +00:00
|
|
|
|
2015-10-06 07:48:39 +00:00
|
|
|
alias_attribute :content, :body
|
|
|
|
|
2015-12-26 07:10:24 +00:00
|
|
|
if ::ActiveRecord::VERSION::STRING >= '3.1'
|
2014-09-03 09:18:42 +00:00
|
|
|
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
|
|
|
|
|
2015-03-25 10:06:59 +00:00
|
|
|
class Recommendation < ActiveRecord::Base
|
|
|
|
belongs_to :person
|
|
|
|
belongs_to :target_person, class_name: 'Person'
|
|
|
|
belongs_to :article
|
|
|
|
end
|
|
|
|
|
2014-01-01 17:53:51 +00:00
|
|
|
module Namespace
|
|
|
|
class Article < ::Article
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-05 09:13:03 +00:00
|
|
|
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
|
2013-08-01 03:19:19 +00:00
|
|
|
ActiveRecord::Migration.verbose = false
|
|
|
|
|
|
|
|
ActiveRecord::Schema.define do
|
2015-11-18 22:51:23 +00:00
|
|
|
create_table :people, force: true do |t|
|
2013-08-01 03:19:19 +00:00
|
|
|
t.integer :parent_id
|
|
|
|
t.string :name
|
2013-11-06 05:37:00 +00:00
|
|
|
t.string :email
|
2013-12-07 00:51:55 +00:00
|
|
|
t.string :only_search
|
|
|
|
t.string :only_sort
|
|
|
|
t.string :only_admin
|
2014-10-24 08:41:37 +00:00
|
|
|
t.string :new_start
|
|
|
|
t.string :stop_end
|
2013-08-01 03:19:19 +00:00
|
|
|
t.integer :salary
|
2014-10-18 21:23:03 +00:00
|
|
|
t.date :life_start
|
2013-08-04 13:13:41 +00:00
|
|
|
t.boolean :awesome, default: false
|
2014-10-17 12:02:16 +00:00
|
|
|
t.boolean :terms_and_conditions, default: false
|
2014-10-24 08:41:37 +00:00
|
|
|
t.boolean :true_or_false, default: true
|
2014-09-23 12:06:43 +00:00
|
|
|
t.timestamps null: false
|
2013-08-01 03:19:19 +00:00
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
|
2015-11-18 22:51:23 +00:00
|
|
|
create_table :articles, force: true do |t|
|
2015-03-25 10:06:59 +00:00
|
|
|
t.integer :person_id
|
|
|
|
t.string :title
|
|
|
|
t.text :subject_header
|
|
|
|
t.text :body
|
2015-07-09 20:41:41 +00:00
|
|
|
t.boolean :published, default: true
|
2013-08-01 03:19:19 +00:00
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
|
2015-11-18 22:51:23 +00:00
|
|
|
create_table :comments, force: true do |t|
|
2015-03-25 10:06:59 +00:00
|
|
|
t.integer :article_id
|
|
|
|
t.integer :person_id
|
|
|
|
t.text :body
|
2013-08-01 03:19:19 +00:00
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
|
2015-11-18 22:51:23 +00:00
|
|
|
create_table :tags, force: true do |t|
|
2015-03-25 10:06:59 +00:00
|
|
|
t.string :name
|
2013-08-01 03:19:19 +00:00
|
|
|
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|
|
2015-03-25 10:06:59 +00:00
|
|
|
t.integer :article_id
|
|
|
|
t.integer :tag_id
|
2013-08-01 03:19:19 +00:00
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
|
2015-11-18 22:51:23 +00:00
|
|
|
create_table :notes, force: true do |t|
|
2015-03-25 10:06:59 +00:00
|
|
|
t.integer :notable_id
|
|
|
|
t.string :notable_type
|
|
|
|
t.string :note
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
2013-08-05 22:01:52 +00:00
|
|
|
|
2015-11-18 22:51:23 +00:00
|
|
|
create_table :recommendations, force: true do |t|
|
2015-03-25 10:06:59 +00:00
|
|
|
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
|
2013-08-01 03:19:19 +00:00
|
|
|
end
|
2018-03-15 15:56:08 +00:00
|
|
|
|
|
|
|
module SubDB
|
|
|
|
class Base < ActiveRecord::Base
|
|
|
|
self.abstract_class = true
|
|
|
|
establish_connection(
|
|
|
|
adapter: 'sqlite3',
|
|
|
|
database: ':memory:'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
class OperationHistory < Base
|
|
|
|
end
|
|
|
|
|
|
|
|
module Schema
|
|
|
|
def self.create
|
|
|
|
s = ::ActiveRecord::Schema.new
|
|
|
|
s.instance_variable_set(:@connection, SubDB::Base.connection)
|
|
|
|
s.verbose = false
|
|
|
|
s.define({}) do
|
|
|
|
create_table :operation_histories, force: true do |t|
|
|
|
|
t.string :operation_type
|
|
|
|
t.integer :people_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|