Skip to content

Commit

Permalink
Fix minor spec failures with Mongoid
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Mar 23, 2012
1 parent 2ccbd16 commit 3f9b456
Show file tree
Hide file tree
Showing 24 changed files with 232 additions and 164 deletions.
2 changes: 1 addition & 1 deletion app/views/rails_admin/main/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
- unless params[:all]
- total_count = @objects.total_count
= paginate(@objects, :theme => 'twitter-bootstrap', :remote => true)
= link_to(t("admin.misc.show_all"), index_path(params.merge(:all => true)), :class => "show-all btn clearfix pjax") unless total_count > 100 || total_count <= @objects.size
= link_to(t("admin.misc.show_all"), index_path(params.merge(:all => true)), :class => "show-all btn clearfix pjax") unless total_count > 100 || total_count <= @objects.to_a.size
.clearfix.total-count= "#{total_count} #{@model_config.label_plural.downcase}"
- else
.clearfix.total-count= "#{@objects.size} #{@model_config.label_plural.downcase}"
18 changes: 12 additions & 6 deletions lib/rails_admin/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def properties
@properties if @properties
@properties = model.fields.map do |name,field|
ar_type =
if name == '_type'
if name == '_id'
{ :type => :bson_object_id, :length => nil, :serial? => true }
elsif name == '_type'
{ :type => :mongoid_type, :length => 1024 }
elsif field.type.to_s == 'String'
if (length = length_validation_lookup(name)) && length < 256
Expand All @@ -92,7 +94,7 @@ def properties
else
{
"Array" => { :type => :serialized, :length => nil },
"BigDecimal" => { :type => :string, :length => 1024 },
"BigDecimal" => { :type => :decimal, :length => nil },
"Boolean" => { :type => :boolean, :length => nil },
"BSON::ObjectId" => { :type => :bson_object_id, :length => nil },
"Date" => { :type => :date, :length => nil },
Expand Down Expand Up @@ -216,7 +218,7 @@ def build_statement(column, type, value, operator)
when :boolean
return { column => false } if ['false', 'f', '0'].include?(value)
return { column => true } if ['true', 't', '1'].include?(value)
when :integer, :belongs_to_association
when :integer
return if value.blank?
{ column => value.to_i } if value.to_i.to_s == value
when :string, :text
Expand Down Expand Up @@ -247,6 +249,9 @@ def build_statement(column, type, value, operator)
when :enum
return if value.blank?
{ column => { "$in" => Array.wrap(value) } }
when :belongs_to_association, :bson_object_id
object_id = (BSON::ObjectId(value) rescue nil)
{ column => object_id } if object_id
end
end

Expand Down Expand Up @@ -306,7 +311,8 @@ def association_type_lookup(macro)
def length_validation_lookup(name)
shortest = model.validators.select do |validator|
validator.attributes.include?(name.to_sym) &&
validator.class == ActiveModel::Validations::LengthValidator
validator.is_a?(ActiveModel::Validations::LengthValidator) &&
validator.options[:maximum]
end.min{|a, b| a.options[:maximum] <=> b.options[:maximum] }
if shortest
shortest.options[:maximum]
Expand Down Expand Up @@ -349,9 +355,9 @@ def sort_by(options, scope)
return scope
end
if options[:sort_reverse]
scope.desc field_name
else
scope.asc field_name
else
scope.desc field_name
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/rails_admin/config/fields/types/bson_object_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class BsonObjectId < RailsAdmin::Config::Fields::Types::String
true
end

register_instance_option(:sort_reverse?) do
serial?
end

def parse_input(params)
begin
params[name] = (params[name].blank? ? nil : BSON::ObjectId(params[name])) if params[name].is_a?(::String)
Expand Down
17 changes: 8 additions & 9 deletions spec/controllers/main_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
end

it "should paginate" do
controller.list_entries(RailsAdmin.config(Team), :index, nil, false).length.should == 40
controller.list_entries(RailsAdmin.config(Team), :index, nil, true).length.should == 20
controller.list_entries(RailsAdmin.config(Team), :index, nil, false).to_a.length.should == 40
controller.list_entries(RailsAdmin.config(Team), :index, nil, true).to_a.length.should == 20
end
end

Expand All @@ -47,7 +47,7 @@
end
end

controller.list_entries.length.should == @players.size
controller.list_entries.to_a.length.should == @players.size
end

it "scopes associated collection records according to associated_collection_scope" do
Expand All @@ -63,7 +63,7 @@
end
end

controller.list_entries.length.should == 3
controller.list_entries.to_a.length.should == 3
end

it "scopes associated collection records according to bindings" do
Expand All @@ -85,7 +85,7 @@
end
end

controller.list_entries.length.should == @team.revenue
controller.list_entries.to_a.length.should == @team.revenue.to_i
end


Expand All @@ -99,7 +99,7 @@
associated_collection_cache_all false
end
end
controller.list_entries.length.should == 30
controller.list_entries.to_a.length.should == 30

RailsAdmin.config Team do
field :players do
Expand All @@ -112,9 +112,8 @@
end

describe "index" do
it "uses source association's primary key with :compact, not target model's default primary key" do
class TeamWithNumberedPlayers < ActiveRecord::Base
self.table_name = 'teams'
it "uses source association's primary key with :compact, not target model's default primary key", :skip_mongoid => true do
class TeamWithNumberedPlayers < Team
has_many :numbered_players, :class_name => 'Player', :primary_key => :number, :foreign_key => 'team_id'
end
FactoryGirl.create :team
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy_app/app/active_record/fan.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Fan < ActiveRecord::Base
validates_presence_of(:name)
belongs_to :fanable, :polymorphic => true
has_and_belongs_to_many :teams

validates_presence_of(:name)
end
8 changes: 6 additions & 2 deletions spec/dummy_app/app/locales/models.en.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
en:
activerecord:
activerecord: &en_attributes
attributes:
fan:
name: His Name
team:
manager: Team Manager
fans: Some Fans
mongoid:
*en_attributes
fr:
activerecord:
activerecord: &fr_attributes
attributes:
fan:
name: Son nom
team:
manager: Manager de l'équipe
fans: Quelques fans
mongoid:
*fr_attributes
31 changes: 31 additions & 0 deletions spec/dummy_app/app/mongoid/player.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
class Player
include Mongoid::Document
include Mongoid::Timestamps

field :deleted_at, :type => DateTime
belongs_to :team, :inverse_of => :players
field :name, :type => String
field :position, :type => String
field :number, :type => Integer
field :retired, :type => Boolean
field :injured, :type => Boolean
field :born_on, :type => Date
field :notes, :type => String
field :suspended, :type => Boolean

validates_presence_of(:name)
validates_numericality_of(:number, :only_integer => true)
validates_uniqueness_of(:number, :scope => :team_id, :message => "There is already a player with that number on this team")

validates_each :name do |record, attr, value|
record.errors.add(:base, "Player is cheating") if value.to_s =~ /on steroids/
end

has_one :draft, :dependent => :destroy
has_many :comments, :as => :commentable
attr_protected :suspended

def draft_id
self.draft.try :id
end

def draft_id=(id)
self.draft = Draft.where(:_id => id).first
end
end
5 changes: 5 additions & 0 deletions spec/dummy_app/app/mongoid/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class Team
validates_numericality_of :losses, :only_integer => true
validates_numericality_of :win_percentage
validates_numericality_of :revenue, :allow_nil => true
# needed to force these attributes to :string type
validates_length_of :logo_url, :maximum => 255
validates_length_of :manager, :maximum => 255
validates_length_of :ballpark, :maximum => 255
validates_length_of :mascot, :maximum => 255

def player_names_truncated
players.map{|p| p.name}.join(", ")[0..32]
Expand Down
8 changes: 3 additions & 5 deletions spec/dummy_app/app/mongoid/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ class User
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

field :roles, :type => Array

# Setup accessible (or protected) attributes for your model
#attr_accessible :email, :password, :password_confirmation, :remember_me, :roles, :avatar

include Mongoid::Timestamps

# Add Paperclip support for avatars
has_mongoid_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }

field :roles, :type => Array

def attr_accessible_role
:custom_role
end
Expand Down
5 changes: 4 additions & 1 deletion spec/integration/authorization/cancan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def initialize(user)
subject { page }

before(:each) do
RailsAdmin.config{|c| c.authorize_with(:cancan); c.audit_with(:history, User) }
RailsAdmin.config do |c|
c.authorize_with(:cancan)
c.audit_with(:history, User) if CI_ORM == :active_record
end
@user = FactoryGirl.create :user
login_as @user
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require 'spec_helper'
require 'rails_admin/extensions/history/history'

describe "RailsAdmin Basic Bulk Destroy" do
subject { page }

describe "successful bulk delete of records" do
describe "successful bulk delete of records", :active_record => true do
before do
RailsAdmin::History.destroy_all
RailsAdmin.config { |c| c.audit_with :history }
Expand All @@ -30,7 +29,6 @@

describe "cancelled bulk_deletion" do
before do
RailsAdmin::History.destroy_all
@players = 3.times.map { FactoryGirl.create(:player) }
@delete_ids = @players[0..1].map(&:id)
page.driver.post(bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => @delete_ids))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
@player.comments = (@comments = 2.times.map { FactoryGirl.create(:comment) })
@player.save

@abstract_model = RailsAdmin::AbstractModel.new(Player)

# removed schema=>only=>created_at
@non_default_schema = {
"only"=>["id", "updated_at", "deleted_at", "name", "position", "number", "retired", "injured", "born_on", "notes", "suspended"],
"only"=>[@abstract_model.primary_key, "updated_at", "deleted_at", "name", "position", "number", "retired", "injured", "born_on", "notes", "suspended"],
"include"=>{
"team"=>{"only"=>["id", "created_at", "updated_at", "name", "logo_url", "manager", "ballpark", "mascot", "founded", "wins", "losses", "win_percentage", "revenue", "color"]},
"draft"=>{"only"=>["id", "created_at", "updated_at", "date", "round", "pick", "overall", "college", "notes"]},
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/basic/list/rails_admin_basic_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
should have_no_content(@players[3].name)
end

it "should allow to search over more than one attribute" do
it "should allow to search over more than one attribute", :focus => true do
RailsAdmin.config Player do
list do
field :id
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/basic/show/rails_admin_basic_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
before(:each) do
@player = FactoryGirl.create :player
@team = FactoryGirl.create :team
@player.update_attribute(:team, @team)
@player.update_attribute(:team_id, @team.id)
visit show_path(:model_name => "player", :id => @player.id)
end

Expand Down
14 changes: 7 additions & 7 deletions spec/integration/basic/update/rails_admin_basic_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
end

describe "update with has-many association" do
it "should be fillable and emptyable" do
it "should be fillable and emptyable", :active_record => true do
RailsAdmin.config do |c|
c.audit_with :history
end
Expand Down Expand Up @@ -156,14 +156,14 @@

describe "update with serialized objects of Mongoid", :mongoid => true do
before(:each) do
@field_test = FactoryGirl.create :mongoid_field_test
@field_test = FactoryGirl.create :field_test

visit edit_path(:model_name => "mongoid_field_test", :id => @field_test.id)
visit edit_path(:model_name => "field_test", :id => @field_test.id)
end

it "should save the serialized data" do
fill_in "mongoid_field_test[array_field]", :with => "[4, 2]"
fill_in "mongoid_field_test[hash_field]", :with => "{ a: 6, b: 2 }"
fill_in "field_test[array_field]", :with => "[4, 2]"
fill_in "field_test[hash_field]", :with => "{ a: 6, b: 2 }"
click_button "Save"

@field_test.reload
Expand All @@ -172,8 +172,8 @@
end

it "should clear data when empty string is passed" do
fill_in "mongoid_field_test[array_field]", :with => ""
fill_in "mongoid_field_test[hash_field]", :with => ""
fill_in "field_test[array_field]", :with => ""
fill_in "field_test[hash_field]", :with => ""
click_button "Save"

@field_test.reload
Expand Down
Loading

0 comments on commit 3f9b456

Please sign in to comment.