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.
Files changed (43) hide show
  1. data/Gemfile +3 -0
  2. data/README.md +19 -69
  3. data/Rakefile +6 -1
  4. data/app/assets/javascripts/rails_admin/rails_admin.js.erb +10 -5
  5. data/app/controllers/rails_admin/main_controller.rb +9 -7
  6. data/config/initializers/mongoid_extensions.rb +4 -0
  7. data/lib/rails_admin/abstract_model.rb +55 -1
  8. data/lib/rails_admin/adapters/active_record.rb +20 -38
  9. data/lib/rails_admin/adapters/mongoid.rb +333 -0
  10. data/lib/rails_admin/adapters/mongoid/abstract_object.rb +32 -0
  11. data/lib/rails_admin/adapters/mongoid/extension.rb +27 -0
  12. data/lib/rails_admin/config/fields/base.rb +4 -4
  13. data/lib/rails_admin/config/fields/factories/belongs_to_association.rb +5 -2
  14. data/lib/rails_admin/config/fields/factories/serialized.rb +1 -1
  15. data/lib/rails_admin/config/fields/types.rb +2 -1
  16. data/lib/rails_admin/config/fields/types/all.rb +2 -0
  17. data/lib/rails_admin/config/fields/types/belongs_to_association.rb +1 -1
  18. data/lib/rails_admin/config/fields/types/bson_object_id.rb +42 -0
  19. data/lib/rails_admin/config/fields/types/enum.rb +2 -2
  20. data/lib/rails_admin/config/fields/types/mongoid_type.rb +25 -0
  21. data/lib/rails_admin/config/fields/types/polymorphic_association.rb +1 -1
  22. data/lib/rails_admin/config/fields/types/serialized.rb +1 -1
  23. data/lib/rails_admin/engine.rb +1 -0
  24. data/lib/rails_admin/version.rb +1 -1
  25. data/spec/dummy_app/Gemfile +2 -0
  26. data/spec/dummy_app/app/models/article.rb +9 -0
  27. data/spec/dummy_app/app/models/author.rb +6 -0
  28. data/spec/dummy_app/app/models/mongoid_field_test.rb +22 -0
  29. data/spec/dummy_app/app/models/tag.rb +7 -0
  30. data/spec/dummy_app/config/environments/development.rb +2 -2
  31. data/spec/dummy_app/config/mongoid.yml +17 -0
  32. data/spec/dummy_app/db/seeds.rb +7 -7
  33. data/spec/factories.rb +23 -0
  34. data/spec/integration/basic/create/rails_admin_basic_create_spec.rb +13 -0
  35. data/spec/integration/basic/update/rails_admin_basic_update_spec.rb +28 -0
  36. data/spec/spec_helper.rb +13 -0
  37. data/spec/support/tableless.rb +27 -0
  38. data/spec/unit/adapters/active_record_spec.rb +335 -37
  39. data/spec/unit/adapters/mongoid/abstract_object_spec.rb +30 -0
  40. data/spec/unit/adapters/mongoid_spec.rb +581 -0
  41. data/spec/unit/config/fields/base_spec.rb +9 -0
  42. metadata +280 -44
  43. 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.model.table_name}.#{self.name}", :type => self.type }]
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.model.table_name
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.model.table_name || f.keys.first
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.model.table_name
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 : :belongs_to}_association").new(parent, association[:name], association)
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
- child_columns << RailsAdmin::Config::Fields.default_factory.call(parent, type_colum, fields)
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.model.serialized_attributes.keys.include?(properties[:name].to_s)
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 logger.info "Unsupported field datatype: #{type}"
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.model.table_name => self.method_name}
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
+
@@ -60,7 +60,7 @@ module RailsAdmin
60
60
  [config.abstract_model.model.name, config.abstract_model.to_param]
61
61
  end
62
62
 
63
- Hash[*types.collect { |v|
63
+ ::Hash[*types.collect { |v|
64
64
  [v[0], bindings[:view].index_path(v[1])]
65
65
  }.flatten]
66
66
  end
@@ -13,7 +13,7 @@ module RailsAdmin
13
13
  end
14
14
 
15
15
  def parse_input(params)
16
- params[name] = (params[name].blank? ? nil : YAML.load(params[name]))
16
+ params[name] = (params[name].blank? ? nil : YAML.load(params[name])) if params[name].is_a?(::String)
17
17
  end
18
18
  end
19
19
  end
@@ -4,6 +4,7 @@ require 'remotipart'
4
4
  require 'bootstrap-sass'
5
5
  require 'kaminari'
6
6
  require 'rack-pjax'
7
+ require 'jquery-ui-rails'
7
8
  require 'nested_form'
8
9
  require 'rails_admin'
9
10
 
@@ -2,5 +2,5 @@ module RailsAdmin
2
2
  # FIXME: this needs to be manually kept in sync with the gemspec's
3
3
  # s.version until http://jira.codehaus.org/browse/JRUBY-5319 is
4
4
  # fixed
5
- VERSION = "0.0.3"
5
+ VERSION = "0.0.4"
6
6
  end
@@ -2,6 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  gem 'rails', '~> 3.2'
4
4
  gem 'devise', '~> 2.0'
5
+ gem 'bson_ext'
6
+ gem 'mongoid'
5
7
  gem 'rails_admin', :path => '../../'
6
8
  gem 'mlb', '~> 0.5'
7
9
  gem 'paperclip', '~> 2.4'
@@ -0,0 +1,9 @@
1
+ class Article
2
+ include Mongoid::Document
3
+
4
+ field :title, :type => String
5
+ field :body, :type => String
6
+
7
+ referenced_in :author
8
+ references_and_referenced_in_many :tags
9
+ end
@@ -0,0 +1,6 @@
1
+ class Author
2
+ include Mongoid::Document
3
+
4
+ field :name, :type => String
5
+ references_many :articles
6
+ end
@@ -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
@@ -0,0 +1,7 @@
1
+ class Tag
2
+ include Mongoid::Document
3
+
4
+ field :name, :type => String
5
+
6
+ references_and_referenced_in_many :articles
7
+ 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 = false
33
+ config.assets.compress = true
34
34
 
35
35
  # Expands the lines which load the assets
36
- config.assets.debug = true
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
@@ -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 = RailsAdmin::AbstractModel.new("League").first(:conditions => ["name = ?", mlb_team.league])
7
- league = RailsAdmin::AbstractModel.new("League").create(:name => mlb_team.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 = RailsAdmin::AbstractModel.new("Division").first(:conditions => ["name = ?", mlb_team.division])
10
- division = RailsAdmin::AbstractModel.new("Division").create(:name => mlb_team.division, :league => league)
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 = RailsAdmin::AbstractModel.new("Team").first(:conditions => ["name = ?", mlb_team.name])
13
- team = RailsAdmin::AbstractModel.new("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)
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
- RailsAdmin::AbstractModel.new("Player").create(:name => player.name, :number => player.number, :position => player.position, :team => team)
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