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