Skip to content

Commit

Permalink
activeadmin 1.3 support (#125)
Browse files Browse the repository at this point in the history
* Upgrade activeadmin to 1.2.x
* Update ransack to < 1.8.7
* active_admin 1.3 support
* update readme; set activeadmin 1.3
  • Loading branch information
AnatolyShirykalov authored and boie0025 committed Aug 1, 2018
1 parent 0a37baf commit 89bcd52
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 43 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ source 'https://rubygems.org'
# development dependencies will be added by default to the :development group.
gemspec

gem 'ransack', github: 'activerecord-hackery/ransack', ref: 'c869fc210500'
gem 'ransack', github: 'activerecord-hackery/ransack'
gem 'ransack-mongoid', github: 'activerecord-hackery/ransack-mongoid'

# Test app stuff

Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ActiveAdmin is holding off on pulling Mongoid support into the core ActiveAdmin
* Ruby 2.2.2 or greater. (Note, ruby-2.4.0 fails specs, see [this issue](https://github.com/DatabaseCleaner/database_cleaner/issues/466))
* Requires Rails 5.0.x (also tested working on Rails 5.1.x)
* Mongoid 6.x (**WARNING**:, using a Mongoid version >= 6.1.x has resulted in a fair amount of errors seen in the wild. Test your upgrade very carefully with any apps in which you're using this gem with Mongoid >= 6.1.x!)
* ActiveAdmin 1.1 (Not working above 1.1.0 at this time)
* ActiveAdmin 1.3

## Previous versions
* Rails 4.x with Mongoid 5.x use branch rails4-mongoid5
Expand Down Expand Up @@ -45,8 +45,6 @@ You can safely remove the following lines, since are already activeadmin-mongoid

```ruby
gem 'activeadmin'
gem 'meta_search', '>= 1.1.0.pre'
gem 'sass-rails', ['~> 3.1', '>= 3.1.4']
```

### Remove Application Dependencies
Expand Down
5 changes: 2 additions & 3 deletions activeadmin-mongoid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ Gem::Specification.new do |gem|
gem.license = 'MIT'

gem.add_runtime_dependency 'mongoid', ['~> 6.0']
gem.add_runtime_dependency 'activeadmin', ['<= 1.1.0']
gem.add_runtime_dependency 'activeadmin', '~> 1.3.0'
gem.add_runtime_dependency 'jquery-rails'
gem.add_runtime_dependency 'sass-rails', ['>= 3.1.4', '<= 5.0.6']
gem.add_runtime_dependency 'sass-rails', ['>= 3.1.4']

gem.add_development_dependency 'rspec-rails', '~> 3.6'
gem.add_development_dependency 'simplecov'
end
1 change: 0 additions & 1 deletion lib/active_admin-mongoid.rb

This file was deleted.

9 changes: 8 additions & 1 deletion lib/active_admin/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@
require 'active_admin/mongoid/document'
require 'active_admin/mongoid/helpers/collection'
require 'active_admin/mongoid/criteria'

require 'active_admin/mongoid/order_clause'

require 'active_admin/mongoid/inputs/filters/check_boxes_input'
require 'active_admin/mongoid/inputs/filters/select_input'
require 'active_admin/mongoid/filters/active_filter'
require 'active_admin/mongoid/filters/resource_extension'
require 'active_admin/mongoid/controllers/resource_controller'
require 'active_admin/mongoid/resource/attributes'
require 'active_admin/mongoid/csv_builder'

module ActiveAdmin
module Mongoid
class Railtie < ::Rails::Railtie
Expand Down
15 changes: 15 additions & 0 deletions lib/active_admin/mongoid/controllers/resource_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'active_admin/engine'

ActiveAdmin::Engine.module_eval do

initializer 'active_admin.mongoid.resource_controller' do
class ActiveAdmin::ResourceController
def build_new_resource
scoped_collection.send(
method_for_build,
*resource_params
)
end
end
end
end
27 changes: 27 additions & 0 deletions lib/active_admin/mongoid/csv_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module ActiveAdmin
class CSVBuilder
def build(controller, csv)
@collection = controller.send :find_collection, except: :pagination
columns = exec_columns controller.view_context
options = ActiveAdmin.application.csv_options.merge self.options
bom = options.delete :byte_order_mark
column_names = options.delete(:column_names) { true }
csv_options = options.except :encoding_options, :humanize_name

csv << bom if bom

if column_names
csv << CSV.generate_line(columns.map{ |c| encode c.name, options }, csv_options)
end

(1..paginated_collection.total_pages).each do |page|
paginated_collection(page).each do |resource|
resource = controller.send :apply_decorator, resource
csv << CSV.generate_line(build_row(resource, columns, options), csv_options)
end
end

csv
end
end
end
15 changes: 15 additions & 0 deletions lib/active_admin/mongoid/filters/active_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'active_admin/filters/active_filter'
module ActiveAdmin
module Filters

class ActiveFilter
def related_primary_key
if predicate_association
predicate_association.key
elsif related_class
related_class.key
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/active_admin/mongoid/filters/resource_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'active_admin/filters/resource_extension'

module ActiveAdmin::Filters::ResourceExtension
def default_association_filters
if resource_class.respond_to?(:reflect_on_all_associations)
poly, not_poly = resource_class.reflect_on_all_associations.partition{ |r| r.macro == :belongs_to && r.options[:polymorphic] }

filters = poly.map(&:foreign_type) + not_poly.map(&:name)
filters.map &:to_sym
else
[]
end
end
end
11 changes: 11 additions & 0 deletions lib/active_admin/mongoid/inputs/filters/check_boxes_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'active_admin/inputs/filters/check_boxes_input'

class ActiveAdmin::Inputs::Filters::CheckBoxesInput
def searchable_method_name
if searchable_has_many_through?
"#{reflection.through_reflection.name}_#{reflection.foreign_key}"
else
reflection.key || method
end
end
end
12 changes: 12 additions & 0 deletions lib/active_admin/mongoid/inputs/filters/select_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'active_admin/inputs/filters/select_input'

class ActiveAdmin::Inputs::Filters::SelectInput
def searchable_method_name
name = if searchable_has_many_through?
"#{reflection.through_reflection.name}_#{reflection.foreign_key}"
elsif reflection_searchable?
reflection.key
end
(name == '_id') ? 'id' : name
end
end
4 changes: 4 additions & 0 deletions lib/active_admin/mongoid/order_clause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ def to_sql
def to_mongo_options
{ @column => @order.downcase.to_sym }
end

def apply(chain)
chain.reorder(sql)
end
end
end
23 changes: 23 additions & 0 deletions lib/active_admin/mongoid/resource/attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'active_admin/resource/attributes'

module ActiveAdmin

class Resource
module Attributes
def foreign_methods
@foreign_methods ||= resource_class.reflect_on_all_associations.
select{ |r| r.macro == :belongs_to }.
index_by{ |r| r.foreign_key.to_sym }
end

def primary_col?(c)
c.name == '_id'
end

def sti_col?(c)
false
end
end
end
end

5 changes: 5 additions & 0 deletions spec/features/smoke_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@
end
end

it "builds csv" do
visit '/admin/admin_users.csv'
expect(page.status_code).to eq(200) or eq(304)
end


end
end
Expand Down
4 changes: 2 additions & 2 deletions test_app/Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'

gem 'activeadmin-mongoid', path: '../'
gem 'activeadmin', '<= 1.1.0'
gem 'activeadmin', '~> 1.3.0'

# Test app stuff

Expand All @@ -14,7 +14,7 @@ gem 'devise'
gem 'mongoid', '~> 6.0.3', require: true
gem 'kaminari', '~> 1.0'
gem 'kaminari-mongoid'
gem 'ransack'
gem 'ransack', '< 1.8.7'

# Gems used only for assets and not required
# in production environments by default.
Expand Down
64 changes: 32 additions & 32 deletions test_app/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: ..
specs:
activeadmin-mongoid (0.4.0)
activeadmin (<= 1.1.0)
activeadmin-mongoid (0.5.0)
activeadmin (~> 1.2.0)
jquery-rails
mongoid (~> 6.0.3)
sass-rails (>= 3.1.4, <= 5.0.6)
Expand Down Expand Up @@ -33,13 +33,13 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activeadmin (1.1.0)
activeadmin (1.2.1)
arbre (>= 1.1.1)
coffee-rails
formtastic (~> 3.1)
formtastic_i18n
inherited_resources (~> 1.7)
jquery-rails
jquery-rails (>= 4.2.0)
kaminari (>= 0.15, < 2.0)
railties (>= 4.2, < 5.2)
ransack (~> 1.3)
Expand All @@ -65,7 +65,7 @@ GEM
activesupport (>= 3.0.0)
arel (8.0.0)
bcrypt (3.1.11)
bson (4.2.2)
bson (4.3.0)
builder (3.2.3)
capybara (2.15.1)
addressable
Expand Down Expand Up @@ -94,23 +94,23 @@ GEM
docile (1.1.5)
erubi (1.7.1)
execjs (2.7.0)
ffi (1.9.18)
ffi (1.9.23)
formtastic (3.1.5)
actionpack (>= 3.2.13)
formtastic_i18n (0.6.0)
globalid (0.4.1)
activesupport (>= 4.2.0)
has_scope (0.7.1)
actionpack (>= 4.1, < 5.2)
activesupport (>= 4.1, < 5.2)
i18n (1.0.0)
has_scope (0.7.2)
actionpack (>= 4.1)
activesupport (>= 4.1)
i18n (1.0.1)
concurrent-ruby (~> 1.0)
inherited_resources (1.7.2)
actionpack (>= 3.2, < 5.2.x)
inherited_resources (1.8.0)
actionpack (>= 4.2, <= 5.2)
has_scope (~> 0.6)
railties (>= 3.2, < 5.2.x)
railties (>= 4.2, <= 5.2)
responders
jquery-rails (4.3.1)
jquery-rails (4.3.3)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
Expand All @@ -119,18 +119,18 @@ GEM
jslint (1.2.0)
json
json (2.1.0)
kaminari (1.0.1)
kaminari (1.1.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.0.1)
kaminari-activerecord (= 1.0.1)
kaminari-core (= 1.0.1)
kaminari-actionview (1.0.1)
kaminari-actionview (= 1.1.1)
kaminari-activerecord (= 1.1.1)
kaminari-core (= 1.1.1)
kaminari-actionview (1.1.1)
actionview
kaminari-core (= 1.0.1)
kaminari-activerecord (1.0.1)
kaminari-core (= 1.1.1)
kaminari-activerecord (1.1.1)
activerecord
kaminari-core (= 1.0.1)
kaminari-core (1.0.1)
kaminari-core (= 1.1.1)
kaminari-core (1.1.1)
kaminari-mongoid (1.0.1)
kaminari-core (~> 1.0)
mongoid
Expand All @@ -145,8 +145,8 @@ GEM
mini_mime (0.1.4)
mini_portile2 (2.3.0)
minitest (5.11.3)
mongo (2.4.3)
bson (>= 4.2.1, < 5.0.0)
mongo (2.5.1)
bson (>= 4.3.0, < 5.0.0)
mongoid (6.0.3)
activemodel (~> 5.0)
mongo (~> 2.3)
Expand All @@ -158,7 +158,7 @@ GEM
capybara (~> 2.1)
cliver (~> 0.3.1)
websocket-driver (>= 0.2.0)
polyamorous (1.3.1)
polyamorous (1.3.3)
activerecord (>= 3.0)
pry (0.10.4)
coderay (~> 1.1.0)
Expand Down Expand Up @@ -192,19 +192,19 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (12.3.1)
ransack (1.8.3)
ransack (1.8.6)
actionpack (>= 3.0)
activerecord (>= 3.0)
activesupport (>= 3.0)
i18n
polyamorous (~> 1.3)
rb-fsevent (0.10.2)
polyamorous (~> 1.3.2)
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
sass (3.5.1)
sass (3.5.6)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4)
Expand Down Expand Up @@ -247,7 +247,7 @@ PLATFORMS
ruby

DEPENDENCIES
activeadmin (<= 1.1.0)
activeadmin (~> 1.2.0)
activeadmin-mongoid!
capybara
coffee-rails
Expand All @@ -262,7 +262,7 @@ DEPENDENCIES
poltergeist
pry
rails (~> 5.1.6)
ransack
ransack (< 1.8.7)
sass-rails
simplecov
uglifier
Expand Down
1 change: 1 addition & 0 deletions test_app/config/initializers/active_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,4 @@


end

0 comments on commit 89bcd52

Please sign in to comment.