obitum-rails_admin 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +3 -0
- data/README.md +19 -69
- data/Rakefile +6 -1
- data/app/assets/javascripts/rails_admin/rails_admin.js.erb +10 -5
- data/app/controllers/rails_admin/main_controller.rb +9 -7
- data/config/initializers/mongoid_extensions.rb +4 -0
- data/lib/rails_admin/abstract_model.rb +55 -1
- data/lib/rails_admin/adapters/active_record.rb +20 -38
- data/lib/rails_admin/adapters/mongoid.rb +333 -0
- data/lib/rails_admin/adapters/mongoid/abstract_object.rb +32 -0
- data/lib/rails_admin/adapters/mongoid/extension.rb +27 -0
- data/lib/rails_admin/config/fields/base.rb +4 -4
- data/lib/rails_admin/config/fields/factories/belongs_to_association.rb +5 -2
- data/lib/rails_admin/config/fields/factories/serialized.rb +1 -1
- data/lib/rails_admin/config/fields/types.rb +2 -1
- data/lib/rails_admin/config/fields/types/all.rb +2 -0
- data/lib/rails_admin/config/fields/types/belongs_to_association.rb +1 -1
- data/lib/rails_admin/config/fields/types/bson_object_id.rb +42 -0
- data/lib/rails_admin/config/fields/types/enum.rb +2 -2
- data/lib/rails_admin/config/fields/types/mongoid_type.rb +25 -0
- data/lib/rails_admin/config/fields/types/polymorphic_association.rb +1 -1
- data/lib/rails_admin/config/fields/types/serialized.rb +1 -1
- data/lib/rails_admin/engine.rb +1 -0
- data/lib/rails_admin/version.rb +1 -1
- data/spec/dummy_app/Gemfile +2 -0
- data/spec/dummy_app/app/models/article.rb +9 -0
- data/spec/dummy_app/app/models/author.rb +6 -0
- data/spec/dummy_app/app/models/mongoid_field_test.rb +22 -0
- data/spec/dummy_app/app/models/tag.rb +7 -0
- data/spec/dummy_app/config/environments/development.rb +2 -2
- data/spec/dummy_app/config/mongoid.yml +17 -0
- data/spec/dummy_app/db/seeds.rb +7 -7
- data/spec/factories.rb +23 -0
- data/spec/integration/basic/create/rails_admin_basic_create_spec.rb +13 -0
- data/spec/integration/basic/update/rails_admin_basic_update_spec.rb +28 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/support/tableless.rb +27 -0
- data/spec/unit/adapters/active_record_spec.rb +335 -37
- data/spec/unit/adapters/mongoid/abstract_object_spec.rb +30 -0
- data/spec/unit/adapters/mongoid_spec.rb +581 -0
- data/spec/unit/config/fields/base_spec.rb +9 -0
- metadata +280 -44
- data/app/assets/javascripts/rails_admin/jquery-ui-1.8.16.custom.js +0 -5271
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rails_admin/adapters/active_record/abstract_object'
|
2
|
+
module RailsAdmin
|
3
|
+
module Adapters
|
4
|
+
module Mongoid
|
5
|
+
class AbstractObject < RailsAdmin::Adapters::ActiveRecord::AbstractObject
|
6
|
+
def initialize(object)
|
7
|
+
super
|
8
|
+
object.associations.each do |name, association|
|
9
|
+
if association.macro == :references_many
|
10
|
+
instance_eval <<-RUBY, __FILE__, __LINE__ + 1
|
11
|
+
def #{name.to_s.singularize}_ids
|
12
|
+
#{name}.map{|item| item.id }
|
13
|
+
end
|
14
|
+
|
15
|
+
def #{name.to_s.singularize}_ids=(items)
|
16
|
+
self.#{name} = items.
|
17
|
+
map{|item_id| self.#{name}.klass.find(item_id) rescue nil }.
|
18
|
+
compact
|
19
|
+
end
|
20
|
+
RUBY
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def destroy
|
26
|
+
object.destroy
|
27
|
+
object
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RailsAdmin
|
2
|
+
module Adapters
|
3
|
+
module Mongoid
|
4
|
+
module Extension
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
def self.rails_admin(&block)
|
9
|
+
RailsAdmin::Config.model(self, &block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def rails_admin_default_object_label_method
|
14
|
+
self.new_record? ? "new #{self.class.to_s}" : "#{self.class.to_s} ##{self.id}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def safe_send(value)
|
18
|
+
if self.attributes.find{ |k,v| k.to_s == value.to_s }
|
19
|
+
self.read_attribute(value)
|
20
|
+
else
|
21
|
+
self.send(value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -73,11 +73,11 @@ module RailsAdmin
|
|
73
73
|
register_instance_option :searchable_columns do
|
74
74
|
@searchable_columns ||= case self.searchable
|
75
75
|
when true
|
76
|
-
[{ :column => "#{self.abstract_model.
|
76
|
+
[{ :column => "#{self.abstract_model.table_name}.#{self.name}", :type => self.type }]
|
77
77
|
when false
|
78
78
|
[]
|
79
79
|
when :all # valid only for associations
|
80
|
-
table_name = self.associated_model_config.abstract_model.
|
80
|
+
table_name = self.associated_model_config.abstract_model.table_name
|
81
81
|
self.associated_model_config.list.fields.map { |f| { :column => "#{table_name}.#{f.name}", :type => f.type } }
|
82
82
|
else
|
83
83
|
[self.searchable].flatten.map do |f|
|
@@ -86,13 +86,13 @@ module RailsAdmin
|
|
86
86
|
type = nil
|
87
87
|
elsif f.is_a?(Hash) # <Model|table_name> => <attribute|column>
|
88
88
|
am = f.keys.first.is_a?(Class) && AbstractModel.new(f.keys.first)
|
89
|
-
table_name = am && am.
|
89
|
+
table_name = am && am.table_name || f.keys.first
|
90
90
|
column = f.values.first
|
91
91
|
property = am && am.properties.find{ |p| p[:name] == f.values.first.to_sym }
|
92
92
|
type = property && property[:type]
|
93
93
|
else # <attribute|column>
|
94
94
|
am = (self.association? ? self.associated_model_config.abstract_model : self.abstract_model)
|
95
|
-
table_name = am.
|
95
|
+
table_name = am.table_name
|
96
96
|
column = f
|
97
97
|
property = am.properties.find{ |p| p[:name] == f.to_sym }
|
98
98
|
type = property && property[:type]
|
@@ -4,7 +4,7 @@ require 'rails_admin/config/fields/types/belongs_to_association'
|
|
4
4
|
|
5
5
|
RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
|
6
6
|
if association = parent.abstract_model.associations.find {|a| a[:foreign_key] == properties[:name] }
|
7
|
-
field = RailsAdmin::Config::Fields::Types.load("#{association[:polymorphic] ? :polymorphic : :
|
7
|
+
field = RailsAdmin::Config::Fields::Types.load("#{association[:polymorphic] ? :polymorphic : association[:type]}_association").new(parent, association[:name], association)
|
8
8
|
fields << field
|
9
9
|
|
10
10
|
child_columns = []
|
@@ -13,7 +13,10 @@ RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
|
|
13
13
|
|
14
14
|
if association[:polymorphic]
|
15
15
|
type_colum = parent.abstract_model.properties.find {|p| p[:name].to_s == association[:foreign_type].to_s }
|
16
|
-
|
16
|
+
unless type_field = fields.find{|f| f.name.to_s == type_colum[:name].to_s }
|
17
|
+
type_field = RailsAdmin::Config::Fields.default_factory.call(parent, type_colum, fields)
|
18
|
+
end
|
19
|
+
child_columns << type_field
|
17
20
|
end
|
18
21
|
|
19
22
|
child_columns.each do |child_column|
|
@@ -8,7 +8,7 @@ require 'rails_admin/config/fields/types/serialized'
|
|
8
8
|
# @see RailsAdmin::Config::Fields::Types::Password.column_names
|
9
9
|
# @see RailsAdmin::Config::Fields.register_factory
|
10
10
|
RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
|
11
|
-
if parent.abstract_model.
|
11
|
+
if parent.abstract_model.serialized_attributes.include?(properties[:name].to_s)
|
12
12
|
fields << RailsAdmin::Config::Fields::Types::Serialized.new(parent, properties[:name], properties)
|
13
13
|
true
|
14
14
|
else
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'active_support/core_ext/string/inflections'
|
2
2
|
require 'rails_admin/config/fields'
|
3
|
+
require 'rails_admin/config/fields/association'
|
3
4
|
|
4
5
|
module RailsAdmin
|
5
6
|
module Config
|
@@ -8,7 +9,7 @@ module RailsAdmin
|
|
8
9
|
@@registry = {}
|
9
10
|
|
10
11
|
def self.load(type)
|
11
|
-
@@registry[type.to_sym] or
|
12
|
+
@@registry[type.to_sym] or raise "Unsupported field datatype: #{type}"
|
12
13
|
end
|
13
14
|
|
14
15
|
def self.register(type, klass = nil)
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rails_admin/config/fields/types/belongs_to_association'
|
2
2
|
require 'rails_admin/config/fields/types/boolean'
|
3
|
+
require 'rails_admin/config/fields/types/bson_object_id'
|
3
4
|
require 'rails_admin/config/fields/types/date'
|
4
5
|
require 'rails_admin/config/fields/types/datetime'
|
5
6
|
require 'rails_admin/config/fields/types/decimal'
|
@@ -13,6 +14,7 @@ require 'rails_admin/config/fields/types/has_and_belongs_to_many_association'
|
|
13
14
|
require 'rails_admin/config/fields/types/has_many_association'
|
14
15
|
require 'rails_admin/config/fields/types/has_one_association'
|
15
16
|
require 'rails_admin/config/fields/types/integer'
|
17
|
+
require 'rails_admin/config/fields/types/mongoid_type'
|
16
18
|
require 'rails_admin/config/fields/types/password'
|
17
19
|
require 'rails_admin/config/fields/types/polymorphic_association'
|
18
20
|
require 'rails_admin/config/fields/types/string'
|
@@ -12,7 +12,7 @@ module RailsAdmin
|
|
12
12
|
end
|
13
13
|
|
14
14
|
register_instance_option :sortable do
|
15
|
-
@sortable ||= associated_model_config.abstract_model.properties.map{ |p| p[:name] }.include?(associated_model_config.object_label_method) ? associated_model_config.object_label_method : {self.abstract_model.
|
15
|
+
@sortable ||= associated_model_config.abstract_model.properties.map{ |p| p[:name] }.include?(associated_model_config.object_label_method) ? associated_model_config.object_label_method : {self.abstract_model.table_name => self.method_name}
|
16
16
|
end
|
17
17
|
|
18
18
|
register_instance_option :searchable do
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rails_admin/config/fields/types/string'
|
2
|
+
|
3
|
+
module RailsAdmin
|
4
|
+
module Config
|
5
|
+
module Fields
|
6
|
+
module Types
|
7
|
+
class BsonObjectId < RailsAdmin::Config::Fields::Types::String
|
8
|
+
# Register field type for the type loader
|
9
|
+
RailsAdmin::Config::Fields::Types::register(self)
|
10
|
+
|
11
|
+
register_instance_option(:label) do
|
12
|
+
label = ((@label ||= {})[::I18n.locale] ||= abstract_model.model.human_attribute_name name)
|
13
|
+
label = "_id" if label == ''
|
14
|
+
label
|
15
|
+
end
|
16
|
+
|
17
|
+
register_instance_option(:help) do
|
18
|
+
"BSON::ObjectId"
|
19
|
+
end
|
20
|
+
|
21
|
+
register_instance_option(:read_only) do
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
register_instance_option(:visible?) do
|
26
|
+
@name.to_s != '_id'
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse_input(params)
|
30
|
+
begin
|
31
|
+
params[name] = (params[name].blank? ? nil : BSON::ObjectId(params[name])) if params[name].is_a?(::String)
|
32
|
+
rescue BSON::InvalidObjectId
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
|
@@ -20,9 +20,9 @@ module RailsAdmin
|
|
20
20
|
end
|
21
21
|
|
22
22
|
register_instance_option(:pretty_value) do
|
23
|
-
if enum.is_a?(Hash)
|
23
|
+
if enum.is_a?(::Hash)
|
24
24
|
enum.reject{|k,v| v.to_s != value.to_s}.keys.first.to_s.presence || value.presence || ' - '
|
25
|
-
elsif enum.is_a?(Array) && enum.first.is_a?(Array)
|
25
|
+
elsif enum.is_a?(::Array) && enum.first.is_a?(::Array)
|
26
26
|
enum.find{|e|e[1].to_s == value.to_s}.try(:first).to_s.presence || value.presence || ' - '
|
27
27
|
else
|
28
28
|
value.presence || ' - '
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rails_admin/config/fields/types/string'
|
2
|
+
|
3
|
+
module RailsAdmin
|
4
|
+
module Config
|
5
|
+
module Fields
|
6
|
+
module Types
|
7
|
+
class MongoidType < RailsAdmin::Config::Fields::Types::String
|
8
|
+
# Register field type for the type loader
|
9
|
+
RailsAdmin::Config::Fields::Types::register(self)
|
10
|
+
|
11
|
+
register_instance_option(:label) do
|
12
|
+
"Type"
|
13
|
+
end
|
14
|
+
|
15
|
+
register_instance_option(:visible) do
|
16
|
+
false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
|
data/lib/rails_admin/engine.rb
CHANGED
data/lib/rails_admin/version.rb
CHANGED
data/spec/dummy_app/Gemfile
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
class MongoidFieldTest
|
2
|
+
include Mongoid::Document
|
3
|
+
|
4
|
+
field :name, :type => String
|
5
|
+
field :title, :type => String
|
6
|
+
field :subject, :type => String
|
7
|
+
field :description, :type => String
|
8
|
+
field :short_text, :type => String
|
9
|
+
field :array_field, :type => Array
|
10
|
+
field :big_decimal_field, :type => BigDecimal
|
11
|
+
field :boolean_field, :type => Boolean
|
12
|
+
field :bson_object_id_field, :type => BSON::ObjectId
|
13
|
+
field :date_field, :type => Date
|
14
|
+
field :date_time_field, :type => DateTime
|
15
|
+
field :float_field, :type => Float
|
16
|
+
field :hash_field, :type => Hash
|
17
|
+
field :integer_field, :type => Integer
|
18
|
+
field :time_field, :type => Time
|
19
|
+
field :object_field, :type => Object
|
20
|
+
|
21
|
+
validates :short_text, :length => {:maximum => 255}
|
22
|
+
end
|
@@ -30,8 +30,8 @@ DummyApp::Application.configure do
|
|
30
30
|
config.active_record.auto_explain_threshold_in_seconds = 0.5
|
31
31
|
|
32
32
|
# Do not compress assets
|
33
|
-
config.assets.compress =
|
33
|
+
config.assets.compress = true
|
34
34
|
|
35
35
|
# Expands the lines which load the assets
|
36
|
-
config.assets.debug =
|
36
|
+
config.assets.debug = false
|
37
37
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
defaults: &defaults
|
2
|
+
host: localhost
|
3
|
+
autocreate_indexes: false
|
4
|
+
allow_dynamic_fields: true
|
5
|
+
include_root_in_json: false
|
6
|
+
parameterize_keys: true
|
7
|
+
persist_in_safe_mode: false
|
8
|
+
raise_not_found_error: true
|
9
|
+
reconnect_time: 3
|
10
|
+
|
11
|
+
development:
|
12
|
+
<<: *defaults
|
13
|
+
database: dummy_app_development
|
14
|
+
|
15
|
+
test:
|
16
|
+
<<: *defaults
|
17
|
+
database: dummy_app_test
|
data/spec/dummy_app/db/seeds.rb
CHANGED
@@ -3,16 +3,16 @@ require 'mlb'
|
|
3
3
|
User.create(:email => 'username@example.com', :password => 'password', :password_confirmation => 'password')
|
4
4
|
|
5
5
|
MLB::Team.all.each do |mlb_team|
|
6
|
-
unless league =
|
7
|
-
league =
|
6
|
+
unless league = League.first(:conditions => ["name = ?", mlb_team.league])
|
7
|
+
league = League.create(:name => mlb_team.league)
|
8
8
|
end
|
9
|
-
unless division =
|
10
|
-
division =
|
9
|
+
unless division = Division.first(:conditions => ["name = ?", mlb_team.division])
|
10
|
+
division = Division.create(:name => mlb_team.division, :league => league)
|
11
11
|
end
|
12
|
-
unless team =
|
13
|
-
team =
|
12
|
+
unless team = Team.first(:conditions => ["name = ?", mlb_team.name])
|
13
|
+
team = Team.create(:name => mlb_team.name, :logo_url => mlb_team.logo_url, :manager => mlb_team.manager, :ballpark => mlb_team.ballpark, :mascot => mlb_team.mascot, :founded => mlb_team.founded, :wins => mlb_team.wins, :losses => mlb_team.losses, :win_percentage => ("%.3f" % (mlb_team.wins.to_f / (mlb_team.wins + mlb_team.losses))).to_f, :division => division)
|
14
14
|
end
|
15
15
|
mlb_team.players.reject{|player| player.number.nil?}.each do |player|
|
16
|
-
|
16
|
+
Player.create(:name => player.name, :number => player.number, :position => player.position, :team => team)
|
17
17
|
end
|
18
18
|
end
|
data/spec/factories.rb
CHANGED
@@ -31,6 +31,11 @@ FactoryGirl.define do
|
|
31
31
|
sequence(:name) { |n| "League #{n}" }
|
32
32
|
end
|
33
33
|
|
34
|
+
factory :division do
|
35
|
+
sequence(:custom_league_id)
|
36
|
+
sequence(:name) { |n| "Division #{n}" }
|
37
|
+
end
|
38
|
+
|
34
39
|
factory :fan do
|
35
40
|
sequence(:name) { |n| "Fan #{n}" }
|
36
41
|
end
|
@@ -63,4 +68,22 @@ FactoryGirl.define do
|
|
63
68
|
factory :hardball do
|
64
69
|
color('blue')
|
65
70
|
end
|
71
|
+
|
72
|
+
factory :article do
|
73
|
+
sequence(:title) { |n| "Article #{n}" }
|
74
|
+
end
|
75
|
+
|
76
|
+
factory :author do
|
77
|
+
sequence(:name) { |n| "Author #{n}" }
|
78
|
+
end
|
79
|
+
|
80
|
+
factory :tag do
|
81
|
+
sequence(:name) { |n| "Tag #{n}" }
|
82
|
+
end
|
83
|
+
|
84
|
+
factory :mongoid_field_test do
|
85
|
+
sequence(:name) { |n| "Mongoid Field Test #{n}" }
|
86
|
+
array_field([1])
|
87
|
+
hash_field({:a => 1})
|
88
|
+
end
|
66
89
|
end
|
@@ -138,4 +138,17 @@ describe "RailsAdmin Basic Create" do
|
|
138
138
|
should have_content("Player failed to be created. Player is cheating")
|
139
139
|
end
|
140
140
|
end
|
141
|
+
|
142
|
+
describe "creation of Mongoid habtm model object" do
|
143
|
+
before(:each) do
|
144
|
+
visit new_path(:model_name => "tag")
|
145
|
+
fill_in "tag[name]", :with => "Funny"
|
146
|
+
click_button "Save"
|
147
|
+
@tag = RailsAdmin::AbstractModel.new("Tag").first
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should create an object with correct attributes" do
|
151
|
+
@tag.name.should eql("Funny")
|
152
|
+
end
|
153
|
+
end
|
141
154
|
end
|