Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sferik/rails_admin
Browse files Browse the repository at this point in the history
  • Loading branch information
bbenezech committed Mar 30, 2012
2 parents 989cca8 + 576e3e4 commit 24dd9cc
Show file tree
Hide file tree
Showing 92 changed files with 956 additions and 572 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -23,6 +23,7 @@ spec/dummy_app/db/*.sqlite3
spec/dummy_app/db/*.sqlite3-journal
spec/dummy_app/db/schema.rb
spec/dummy_app/log/*.log
spec/dummy_app/public/uploads
spec/dummy_app/Gemfile.lock
tmp/**/*
/.emacs.desktop
Expand Down
33 changes: 17 additions & 16 deletions .travis.yml
Expand Up @@ -5,55 +5,56 @@ rvm:
- 1.9.3
- jruby-18mode
env:
- "CI_DB_ADAPTER=sqlite3"
- "CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
- "CI_DB_ADAPTER=mysql"
- "CI_ORM=active_record CI_DB_ADAPTER=sqlite3"
- "CI_ORM=active_record CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
- "CI_ORM=active_record CI_DB_ADAPTER=mysql"
- "CI_ORM=mongoid"
gemfile:
- Gemfile
- Gemfile31
language: ruby
matrix:
exclude:
- rvm: 1.8.7
env: "CI_DB_ADAPTER=sqlite3"
env: "CI_ORM=active_record CI_DB_ADAPTER=sqlite3"
gemfile: Gemfile
- rvm: 1.8.7
env: "CI_DB_ADAPTER=sqlite3"
env: "CI_ORM=active_record CI_DB_ADAPTER=sqlite3"
gemfile: Gemfile31
- rvm: 1.8.7
env: "CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
env: "CI_ORM=active_record CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
gemfile: Gemfile
- rvm: 1.8.7
env: "CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
env: "CI_ORM=active_record CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
gemfile: Gemfile31
- rvm: 1.8.7
gemfile: Gemfile31
env: "CI_DB_ADAPTER=mysql"
env: "CI_ORM=active_record CI_DB_ADAPTER=mysql"

- rvm: jruby-18mode
env: "CI_DB_ADAPTER=mysql"
env: "CI_ORM=active_record CI_DB_ADAPTER=mysql"
gemfile: Gemfile
- rvm: jruby-18mode
env: "CI_DB_ADAPTER=mysql"
env: "CI_ORM=active_record CI_DB_ADAPTER=mysql"
gemfile: Gemfile31
- rvm: jruby-18mode
env: "CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
env: "CI_ORM=active_record CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
gemfile: Gemfile
- rvm: jruby-18mode
env: "CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
env: "CI_ORM=active_record CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
gemfile: Gemfile31
- rvm: jruby-18mode
env: "CI_DB_ADAPTER=sqlite3"
env: "CI_ORM=active_record CI_DB_ADAPTER=sqlite3"
gemfile: Gemfile31

- rvm: 1.9.3
env: "CI_DB_ADAPTER=mysql"
env: "CI_ORM=active_record CI_DB_ADAPTER=mysql"
gemfile: Gemfile
- rvm: 1.9.3
env: "CI_DB_ADAPTER=mysql"
env: "CI_ORM=active_record CI_DB_ADAPTER=mysql"
gemfile: Gemfile31
- rvm: 1.9.3
gemfile: Gemfile31
env: "CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
env: "CI_ORM=active_record CI_DB_ADAPTER=postgresql CI_DB_USERNAME=postgres"
notifications:
irc: "irc.freenode.org#railsadmin"
18 changes: 17 additions & 1 deletion Gemfile
Expand Up @@ -3,7 +3,7 @@ source 'https://rubygems.org'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
group :active_record do
platforms :jruby do
case ENV['CI_DB_ADAPTER']
when 'mysql'
Expand All @@ -28,9 +28,21 @@ group :development, :test do
gem 'sqlite3', '~> 1.3'
end
end
gem 'paperclip', '~>2.4'
gem 'carrierwave'
gem 'dragonfly'
end

group :mongoid do
gem 'bson_ext'
gem 'mongoid'
gem 'mongoid-paperclip', :require => 'mongoid_paperclip'
gem 'paperclip', '~>2.4'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
gem 'dragonfly'
end

group :development, :test do
gem 'cancan'
end

Expand All @@ -44,6 +56,10 @@ group :debug do
gem 'ruby-debug19'
gem 'simplecov', :require => false
end

platform :jruby do
gem 'ruby-debug'
end
end

platforms :jruby, :mingw_18, :ruby_18 do
Expand Down
18 changes: 14 additions & 4 deletions Gemfile31
Expand Up @@ -3,13 +3,11 @@ source 'https://rubygems.org'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
gem 'rails', '~> 3.1.3'
gem 'rails', '~> 3.1.3'

group :active_record do
platforms :jruby do
gem 'jruby-openssl', '~> 0.7'
# activerecord-jdbc-adapter does not yet have a rails 3.1 compatible release
gem 'activerecord-jdbc-adapter', :git => 'git://github.com/jruby/activerecord-jdbc-adapter.git'
case ENV['CI_DB_ADAPTER']
when 'mysql'
gem 'activerecord-jdbcmysql-adapter', '~> 1.2'
Expand All @@ -33,9 +31,21 @@ group :development, :test do
gem 'sqlite3', '~> 1.3'
end
end
gem 'paperclip', '~>2.4'
gem 'carrierwave'
gem 'dragonfly'
end

group :mongoid do
gem 'bson_ext'
gem 'mongoid'
gem 'mongoid-paperclip', :require => 'mongoid_paperclip'
gem 'paperclip', '~>2.4'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
gem 'dragonfly'
end

group :development, :test do
gem 'cancan'
end

Expand Down
22 changes: 9 additions & 13 deletions README.md
Expand Up @@ -67,7 +67,7 @@ mount RailsAdmin::Engine => '/admin', :as => 'rails_admin' # Feel free to change

It will also add an intializer that will help you getting started. (head for config/initializers/rails_admin.rb)

Optionally, you may wish to set up [Cancan](https://github.com/ryanb/cancan),
Optionally, you may wish to set up [Cancan](https://github.com/ryanb/cancan),
[PaperTrail](https://github.com/airblade/paper_trail), [CKeditor](https://github.com/galetahub/ckeditor)

More on that in the [Wiki](https://github.com/sferik/rails_admin/wiki)
Expand Down Expand Up @@ -96,11 +96,11 @@ irc.freenode.net](http://webchat.freenode.net/?channels=railsadmin).

If you think you found a bug in RailsAdmin, you can [submit an
issue](https://github.com/sferik/rails_admin#issues).
No feature requests or questions please (the mailing list is
No feature requests or questions please (the mailing list is
active and is the preferred venue for feature requests and questions).

## <a name="contributing"></a>Contributing
In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html),
In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html),
**everyone** is encouraged to help improve this project.

Here are some ways *you* can contribute:
Expand Down Expand Up @@ -128,14 +128,12 @@ operating system. Ideally, a bug report should include a pull request with faili
## <a name="pulls"></a>Submitting a Pull Request
1. Fork the project.
2. Create a topic branch.
3. Implement your feature or bug fix. *NOTE* - there's a small test app located in the
3. Implement your feature or bug fix. *NOTE* - there's a small test app located in the
spec/dummy_app directory that you can use to experiment with rails_admin.
4. Add documentation for your feature or bug fix.
5. Run `bundle exec rake doc:yard`. If your changes are not 100% documented, go back to step 4.
6. Add specs for your feature or bug fix.
7. Run `bundle exec rake spec`. If your changes are not 100% covered, go back to step 6.
8. Commit and push your changes.
9. Submit a pull request. Please do not include changes to the gemspec, version, or history file.
5. Add specs for your feature or bug fix.
6. Commit and push your changes.
7. Submit a pull request. Please do not include changes to the gemspec, version, or history file.
(If you want to create your own version for some reason, please do so in a separate commit.)

## <a name="versions"></a>Supported Ruby Versions
Expand All @@ -145,12 +143,10 @@ This library aims to support and is [tested against][travis] the following Ruby
* Ruby 1.9.2
* Ruby 1.9.3
* [Rubinius][]
* [Ruby Enterprise Edition][ree]
* [JRuby][jruby]
* [JRuby][]

[rubinius]: http://rubini.us/
[ree]: http://www.rubyenterpriseedition.com/
[jruby]:http://jruby.org/
[jruby]: http://jruby.org/

## <a name="screenshots"></a>Screenshots
![Dashboard view](https://github.com/sferik/rails_admin/raw/master/screenshots/dashboard.png "dashboard view")
Expand Down
2 changes: 1 addition & 1 deletion app/views/rails_admin/main/index.html.haml
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}"
25 changes: 18 additions & 7 deletions lib/rails_admin/adapters/mongoid.rb
Expand Up @@ -12,7 +12,12 @@ def new(params = {})
end

def get(id)
if object = model.where(:_id=>BSON::ObjectId(id)).first
begin
id = BSON::ObjectId(id) unless id.is_a?(BSON::ObjectId)
rescue BSON::InvalidObjectId
return nil
end
if object = model.where(:_id=>id).first
AbstractObject.new object
else
nil
Expand Down Expand Up @@ -74,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 @@ -87,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 @@ -211,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 @@ -242,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 @@ -301,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 @@ -344,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
24 changes: 20 additions & 4 deletions lib/rails_admin/adapters/mongoid/abstract_object.rb
Expand Up @@ -12,10 +12,26 @@ def #{name.to_s.singularize}_ids
#{name}.map{|item| item.id }
end
def #{name.to_s.singularize}_ids=(items)
self.#{name} = items.
map{|item_id| self.#{name}.klass.find(item_id) rescue nil }.
compact
def #{name.to_s.singularize}_ids=(item_ids)
items = (#{name}.klass.find(Array.wrap(item_ids)) rescue [])
if persisted?
#{name}.substitute items
else
items.each do |item|
item.update_attribute('#{association.foreign_key}', id)
end
end
end
RUBY
elsif association.macro == :references_one
instance_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name.to_s}_id=(item_id)
item = (#{association.klass}.find(item_id) rescue nil)
if persisted?
self.#{name} = item
else
item.update_attribute('#{association.foreign_key}', id) if item
end
end
RUBY
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/adapters/mongoid/extension.rb
Expand Up @@ -34,7 +34,7 @@ module NestedAttributesExtension
# Mongoid accepts_nested_attributes_for does not store options in accessible scope,
# so we intercept the call and store it in instance variable which can be accessed from outside
def accepts_nested_attributes_for_with_rails_admin(*args)
@nested_attributes_options = {}
@nested_attributes_options ||= {}
options = args.extract_options!
args.each do |arg|
@nested_attributes_options[arg.to_sym] = options.reverse_merge(:allow_destroy=>false, :update_only=>false)
Expand Down
4 changes: 3 additions & 1 deletion lib/rails_admin/config/actions/dashboard.rb
Expand Up @@ -25,7 +25,9 @@ class Dashboard < RailsAdmin::Config::Actions::Base
current_count = t.count({}, scope)
@max = current_count > @max ? current_count : @max
@count[t.pretty_name] = current_count
@most_recent_changes[t.pretty_name] = t.model.order("updated_at desc").first.try(:updated_at) rescue nil
if t.properties.find{|c| c[:name] == :updated_at }
@most_recent_changes[t.pretty_name] = t.first(:sort => :updated_at).try(:updated_at)
end
end
render @action.template_name, :status => (flash[:error].present? ? :not_found : 200)
end
Expand Down
23 changes: 20 additions & 3 deletions lib/rails_admin/config/fields/base.rb
Expand Up @@ -149,7 +149,7 @@ def virtual?
#
register_instance_option :valid_length do
@valid_length ||= abstract_model.model.validators_on(name).find{|v|
v.is_a?(ActiveModel::Validations::LengthValidator)}.try{|v| v.options} || {}
v.kind == :length }.try{|v| v.options} || {}
end

register_instance_option :partial do
Expand All @@ -162,8 +162,8 @@ def virtual?
register_instance_option :required? do
@required ||= !!([name] + children_fields).uniq.find do |column_name|
!!abstract_model.model.validators_on(column_name).find do |v|
v.is_a?(ActiveModel::Validations::PresenceValidator) && !v.options[:allow_nil] ||
v.is_a?(ActiveModel::Validations::NumericalityValidator) && !v.options[:allow_nil]
v.kind == :presence && !v.options[:allow_nil] ||
v.kind == :numericality && !v.options[:allow_nil]
end
end
end
Expand Down Expand Up @@ -275,6 +275,23 @@ def method_name
def html_default_value
bindings[:object].new_record? && self.value.nil? && !self.default_value.nil? ? self.default_value : nil
end

def inspect
"#<#{self.class.name}[#{name}] #{
instance_variables.map do |v|
value = instance_variable_get(v)
if [:@parent, :@root, :@section].include? v
if value.respond_to? :name
"#{v}=#{value.name.inspect}"
else
"#{v}=#{value.class.name}"
end
else
"#{v}=#{value.inspect}"
end
end.join(", ")
}>"
end
end
end
end
Expand Down

0 comments on commit 24dd9cc

Please sign in to comment.