mirror of
https://github.com/activerecord-hackery/ransack.git
synced 2022-11-09 13:47:45 -05:00
specs set up for mongoid
This commit is contained in:
parent
b3765cc060
commit
9a796f157e
5 changed files with 287 additions and 0 deletions
6
Rakefile
6
Rakefile
|
@ -4,6 +4,12 @@ require 'rspec/core/rake_task'
|
|||
Bundler::GemHelper.install_tasks
|
||||
|
||||
RSpec::Core::RakeTask.new(:spec) do |rspec|
|
||||
ENV['SPEC'] = 'spec/ransack/**/*_spec.rb'
|
||||
rspec.rspec_opts = ['--backtrace']
|
||||
end
|
||||
|
||||
RSpec::Core::RakeTask.new(:mongoid) do |rspec|
|
||||
ENV['SPEC'] = 'spec/mongoid/**/*_spec.rb'
|
||||
rspec.rspec_opts = ['--backtrace']
|
||||
end
|
||||
|
||||
|
|
68
spec/mongoid/configuration_spec.rb
Normal file
68
spec/mongoid/configuration_spec.rb
Normal file
|
@ -0,0 +1,68 @@
|
|||
require 'mongoid_spec_helper'
|
||||
|
||||
module Ransack
|
||||
describe Configuration do
|
||||
it 'yields Ransack on configure' do
|
||||
Ransack.configure do |config|
|
||||
expect(config).to eq Ransack
|
||||
end
|
||||
end
|
||||
|
||||
it 'adds predicates' do
|
||||
Ransack.configure do |config|
|
||||
config.add_predicate :test_predicate
|
||||
end
|
||||
|
||||
expect(Ransack.predicates).to have_key 'test_predicate'
|
||||
expect(Ransack.predicates).to have_key 'test_predicate_any'
|
||||
expect(Ransack.predicates).to have_key 'test_predicate_all'
|
||||
end
|
||||
|
||||
it 'avoids creating compound predicates if compounds: false' do
|
||||
Ransack.configure do |config|
|
||||
config.add_predicate(
|
||||
:test_predicate_without_compound,
|
||||
:compounds => false
|
||||
)
|
||||
end
|
||||
expect(Ransack.predicates)
|
||||
.to have_key 'test_predicate_without_compound'
|
||||
expect(Ransack.predicates)
|
||||
.not_to have_key 'test_predicate_without_compound_any'
|
||||
expect(Ransack.predicates)
|
||||
.not_to have_key 'test_predicate_without_compound_all'
|
||||
end
|
||||
|
||||
it 'should have default value for search key' do
|
||||
expect(Ransack.options[:search_key]).to eq :q
|
||||
end
|
||||
|
||||
it 'changes default search key parameter' do
|
||||
# store original state so we can restore it later
|
||||
before = Ransack.options.clone
|
||||
|
||||
Ransack.configure do |config|
|
||||
config.search_key = :query
|
||||
end
|
||||
|
||||
expect(Ransack.options[:search_key]).to eq :query
|
||||
|
||||
# restore original state so we don't break other tests
|
||||
Ransack.options = before
|
||||
end
|
||||
|
||||
it 'adds predicates that take arrays, overriding compounds' do
|
||||
Ransack.configure do |config|
|
||||
config.add_predicate(
|
||||
:test_array_predicate,
|
||||
:wants_array => true,
|
||||
:compounds => true
|
||||
)
|
||||
end
|
||||
|
||||
expect(Ransack.predicates['test_array_predicate'].wants_array).to eq true
|
||||
expect(Ransack.predicates).not_to have_key 'test_array_predicate_any'
|
||||
expect(Ransack.predicates).not_to have_key 'test_array_predicate_all'
|
||||
end
|
||||
end
|
||||
end
|
6
spec/mongoid/support/mongoid.yml
Normal file
6
spec/mongoid/support/mongoid.yml
Normal file
|
@ -0,0 +1,6 @@
|
|||
test:
|
||||
sessions:
|
||||
default:
|
||||
database: ransack_mongoid_test
|
||||
hosts:
|
||||
- localhost:27017
|
148
spec/mongoid/support/schema.rb
Normal file
148
spec/mongoid/support/schema.rb
Normal file
|
@ -0,0 +1,148 @@
|
|||
require 'mongoid'
|
||||
|
||||
Mongoid.load!(File.expand_path("../mongoid.yml", __FILE__), :test)
|
||||
|
||||
class Person
|
||||
include Mongoid::Document
|
||||
include Mongoid::Timestamps
|
||||
|
||||
field :name, type: String
|
||||
field :email, type: String
|
||||
field :only_search, type: String
|
||||
field :only_sort, type: String
|
||||
field :only_admin, type: String
|
||||
field :salary, type: Integer
|
||||
field :awesome, type: Boolean, default: false
|
||||
|
||||
belongs_to :parent, :class_name => 'Person', inverse_of: :children
|
||||
has_many :children, :class_name => 'Person', inverse_of: :parent
|
||||
|
||||
has_many :articles
|
||||
has_many :comments
|
||||
|
||||
# has_many :authored_article_comments, :through => :articles,
|
||||
# :source => :comments, :foreign_key => :person_id
|
||||
|
||||
has_many :notes, :as => :notable
|
||||
|
||||
default_scope -> { order(id: :desc) }
|
||||
|
||||
scope :restricted, lambda { where("restricted = 1") }
|
||||
scope :active, lambda { where("active = 1") }
|
||||
scope :over_age, lambda { |y| where(["age > ?", y]) }
|
||||
|
||||
ransacker :reversed_name, :formatter => proc { |v| v.reverse } do |parent|
|
||||
parent.table[:name]
|
||||
end
|
||||
|
||||
# ransacker :doubled_name do |parent|
|
||||
# Arel::Nodes::InfixOperation.new(
|
||||
# '||', parent.table[:name], parent.table[:name]
|
||||
# )
|
||||
# end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
class Article
|
||||
include Mongoid::Document
|
||||
|
||||
field :person_id, type: Integer
|
||||
field :title, type: String
|
||||
field :body, type: String
|
||||
|
||||
belongs_to :person
|
||||
has_many :comments
|
||||
# has_and_belongs_to_many :tags
|
||||
has_many :notes, :as => :notable
|
||||
end
|
||||
|
||||
module Namespace
|
||||
class Article < ::Article
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
class Comment
|
||||
include Mongoid::Document
|
||||
|
||||
field :article_id, type: Integer
|
||||
field :person_id, type: Integer
|
||||
field :body, type: String
|
||||
|
||||
|
||||
belongs_to :article
|
||||
belongs_to :person
|
||||
end
|
||||
|
||||
class Tag
|
||||
include Mongoid::Document
|
||||
|
||||
field :name, type: String
|
||||
|
||||
# has_and_belongs_to_many :articles
|
||||
end
|
||||
|
||||
class Note
|
||||
include Mongoid::Document
|
||||
|
||||
field :notable_id, type: Integer
|
||||
field :notable_type, type: String
|
||||
field :note, type: String
|
||||
|
||||
belongs_to :notable, :polymorphic => true
|
||||
end
|
||||
|
||||
module Schema
|
||||
def self.create
|
||||
10.times do
|
||||
person = Person.create!
|
||||
Note.create!(:notable => person)
|
||||
3.times do
|
||||
article = Article.create!(:person => person)
|
||||
3.times do
|
||||
# article.tags = [Tag.create!, Tag.create!, Tag.create!]
|
||||
end
|
||||
Note.create!(:notable => article)
|
||||
10.times do
|
||||
Comment.create!(:article => article, :person => person)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Comment.create!(
|
||||
:body => 'First post!',
|
||||
:article => Article.create!(:title => 'Hello, world!')
|
||||
)
|
||||
end
|
||||
end
|
59
spec/mongoid_spec_helper.rb
Normal file
59
spec/mongoid_spec_helper.rb
Normal file
|
@ -0,0 +1,59 @@
|
|||
# require 'machinist/active_record'
|
||||
require 'sham'
|
||||
require 'faker'
|
||||
require 'pry'
|
||||
require 'mongoid'
|
||||
require 'ransack'
|
||||
|
||||
I18n.enforce_available_locales = false
|
||||
Time.zone = 'Eastern Time (US & Canada)'
|
||||
I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'support', '*.yml')]
|
||||
|
||||
Dir[File.expand_path('../{helpers,mongoid/support}/*.rb', __FILE__)]
|
||||
.each do |f|
|
||||
require f
|
||||
end
|
||||
|
||||
Sham.define do
|
||||
name { Faker::Name.name }
|
||||
title { Faker::Lorem.sentence }
|
||||
body { Faker::Lorem.paragraph }
|
||||
salary { |index| 30000 + (index * 1000) }
|
||||
tag_name { Faker::Lorem.words(3).join(' ') }
|
||||
note { Faker::Lorem.words(7).join(' ') }
|
||||
only_admin { Faker::Lorem.words(3).join(' ') }
|
||||
only_search { Faker::Lorem.words(3).join(' ') }
|
||||
only_sort { Faker::Lorem.words(3).join(' ') }
|
||||
notable_id { |id| id }
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.alias_it_should_behave_like_to :it_has_behavior, 'has behavior'
|
||||
|
||||
config.before(:suite) do
|
||||
puts '=' * 80
|
||||
connection_name = Mongoid.default_session.inspect
|
||||
puts "Running specs against #{connection_name}, Mongoid #{
|
||||
Mongoid::VERSION}, Moped #{Moped::VERSION} and Origin #{Origin::VERSION}..."
|
||||
puts '=' * 80
|
||||
Schema.create
|
||||
end
|
||||
|
||||
config.before(:all) { Sham.reset(:before_all) }
|
||||
config.before(:each) { Sham.reset(:before_each) }
|
||||
|
||||
config.include RansackHelper
|
||||
end
|
||||
|
||||
RSpec::Matchers.define :be_like do |expected|
|
||||
match do |actual|
|
||||
actual.gsub(/^\s+|\s+$/, '').gsub(/\s+/, ' ').strip ==
|
||||
expected.gsub(/^\s+|\s+$/, '').gsub(/\s+/, ' ').strip
|
||||
end
|
||||
end
|
||||
|
||||
RSpec::Matchers.define :have_attribute_method do |expected|
|
||||
match do |actual|
|
||||
actual.attribute_method?(expected)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue