activeadmin-mongoidv3 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activeadmin-mongoid.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Elia Schito
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # ActiveAdmin::Mongoid
2
+
3
+ ActiveAdmin hacks to support Mongoid.
4
+ Some ActiveAdmin features are disabled:
5
+
6
+ - comments
7
+
8
+ For more on Mongoid support in ActiveAdmin see [this issue](https://github.com/gregbell/active_admin/issues/26).
9
+
10
+ ## Installation
11
+
12
+ ### Some Gems
13
+ Add the following gems to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'activeadmin-mongoid'
17
+ gem 'devise'
18
+ ```
19
+ Devise is the gem used to managed admin authentication.
20
+ The gem is required to force itself ORM configuration.
21
+ Else the gem will try to use by default ActiveRecord ORM.
22
+
23
+ You can safely remove the following lines, since are already activeadmin-mongoid dependencies:
24
+
25
+ ```ruby
26
+ gem 'activeadmin'
27
+ gem 'meta_search', '>= 1.1.0.pre'
28
+ gem 'sass-rails', ['~> 3.1', '>= 3.1.4']
29
+ ```
30
+
31
+ ### Remove Application Dependencies
32
+ In your config/application.rb, replace :
33
+
34
+ ```ruby
35
+ require 'rails/all'
36
+ ```
37
+
38
+ with :
39
+
40
+ ```ruby
41
+ require "action_controller/railtie"
42
+ require "action_mailer/railtie"
43
+ require "active_resource/railtie"
44
+ require "sprockets/railtie"
45
+ require "rails/test_unit/railtie"
46
+ ```
47
+
48
+ rails/all includes elements requiring ActiveRecord::Connection ...
49
+
50
+ ### Bundle & Crank
51
+
52
+ Execute:
53
+
54
+ $ bundle
55
+ $ rails g devise:install
56
+ $ rails g active_admin:install
57
+
58
+ Check that the generated initializers/devise.rb file requires mongoid orm.
59
+ You may find a line like this :
60
+
61
+ ```ruby
62
+ require 'devise/orm/mongoid'
63
+ ```
64
+
65
+ Then create the admin user:
66
+
67
+ $ rails console
68
+ >> AdminUser.create :email => 'admin@example.com', :password => 'password', :password_confirmation => 'password'
69
+
70
+ And that's pretty much it !
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
79
+
80
+ ## Copyright
81
+
82
+ Copyright © 2012 Elia Schito. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ require File.expand_path('../lib/active_admin/mongoid/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Elia Schito']
6
+ gem.email = ['elia@schito.me']
7
+ gem.description = %q{ActiveAdmin hacks to support Mongoid (some ActiveAdmin features are disabled)}
8
+ gem.summary = %q{ActiveAdmin hacks to support Mongoid}
9
+ gem.homepage = ''
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = 'activeadmin-mongoidv3'
15
+ gem.require_paths = ['lib']
16
+ gem.version = ActiveAdmin::Mongoid::VERSION
17
+ gem.license = 'MIT'
18
+
19
+ gem.add_runtime_dependency 'mongoid', '>= 2.4'
20
+ gem.add_runtime_dependency 'activeadmin', '~> 0.4'
21
+ gem.add_runtime_dependency 'sass-rails', ['~> 3.1', '>= 3.1.4']
22
+ end
@@ -0,0 +1 @@
1
+ require 'active_admin/mongoid'
@@ -0,0 +1,21 @@
1
+ require 'active_admin'
2
+
3
+ module ActiveAdmin
4
+ module Mongoid
5
+ end
6
+
7
+ class << self
8
+ alias setup_without_mongoid setup
9
+
10
+ # Load monkey patches *after* the setup process
11
+ def setup *args, &block
12
+ setup_without_mongoid *args, &block
13
+
14
+ require 'active_admin/mongoid/adaptor'
15
+ require 'active_admin/mongoid/comments'
16
+ require 'active_admin/mongoid/filter_form_builder'
17
+ require 'active_admin/mongoid/resource'
18
+ require 'active_admin/mongoid/document'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,64 @@
1
+ module ActiveAdmin
2
+ module Mongoid
3
+ module Adaptor
4
+ class Search
5
+ attr_reader :base, :query, :query_hash, :search_params
6
+
7
+ def initialize(object, search_params = {})
8
+ @base = object
9
+ @search_params = search_params
10
+ @query_hash = get_query_hash(search_params)
11
+ @query = @base.where(@query_hash)
12
+ end
13
+
14
+ def respond_to?(method_id)
15
+ @query.send(:respond_to?, method_id)
16
+ end
17
+
18
+ def method_missing(method_id, *args, &block)
19
+ if is_query(method_id)
20
+ @search_params[method_id.to_s]
21
+ else
22
+ @query.send(method_id, *args, &block)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def is_query(method_id)
29
+ method_id.to_s =~ /_(contains|eq|gt|lt|gte|lte)$/
30
+ end
31
+
32
+ def get_query_hash(search_params)
33
+ searches = search_params.map do|k, v|
34
+ mongoidify_search(k,v)
35
+ end
36
+ Hash[searches]
37
+ end
38
+
39
+ def mongoidify_search(k, v)
40
+ case k
41
+ when /_contains$/
42
+ [get_attribute(k, '_contains'), Regexp.new(Regexp.escape("#{v}"), Regexp::IGNORECASE)]
43
+ when /_eq$/
44
+ [get_attribute(k, '_eq'), v]
45
+ when /_gt$/
46
+ [get_attribute(k, "_gt").to_sym.gt, v]
47
+ when /_lt$/
48
+ [get_attribute(k, "_lt").to_sym.lt, v]
49
+ when /_gte$/
50
+ [get_attribute(k, "_gte").to_sym.gte, v]
51
+ when /_lte$/
52
+ [get_attribute(k, "_lte").to_sym.lte, v]
53
+ else
54
+ [k, v]
55
+ end
56
+ end
57
+
58
+ def get_attribute(k, postfix)
59
+ k.match(/^(.*)#{postfix}$/)[1]
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,7 @@
1
+ ActiveAdmin::Namespace # autoload
2
+ class ActiveAdmin::Namespace
3
+ # Disable comments
4
+ def comments?
5
+ false
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ require 'mongoid'
2
+
3
+ module ActiveAdmin::Mongoid::Document
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def content_columns
8
+ @content_columns ||= fields.map(&:second).select {|f| f.name !~ /(^_|^(created|updated)_at)/}
9
+ end
10
+
11
+ def columns
12
+ @columns ||= fields.map(&:second)
13
+ end
14
+
15
+ def reorder *args
16
+ scoped
17
+ end
18
+ end
19
+ end
20
+
21
+ Mongoid::Document.send :include, ActiveAdmin::Mongoid::Document
@@ -0,0 +1,33 @@
1
+ class ActiveAdmin::FilterFormBuilder
2
+ def default_input_type(method, options = {})
3
+ if column = column_for(method)
4
+ case column.type.name.downcase.to_sym
5
+ when :date, :datetime, :time
6
+ return :date_range
7
+ when :string, :text
8
+ return :string
9
+ when :integer
10
+ return :select if reflection_for(method.to_s.gsub('_id','').to_sym)
11
+ return :numeric
12
+ when :float, :decimal
13
+ return :numeric
14
+ end
15
+ elsif is_association?(method)
16
+ return :select
17
+ else # dirty but allows to create filters for hashes
18
+ return :string
19
+ end
20
+ end
21
+
22
+ def is_association?(method)
23
+ @object.klass.associations.to_a.map(&:first).include?(method.to_s)
24
+ end
25
+
26
+ def column_for(method)
27
+ @object.klass.fields[method.to_s] if @object.klass.respond_to?(:fields)
28
+ end
29
+
30
+ def reflection_for(method)
31
+ @object.klass.reflect_on_association(method) if @object.klass.respond_to?(:reflect_on_association)
32
+ end
33
+ end
@@ -0,0 +1,28 @@
1
+ require 'active_admin'
2
+ require 'inherited_resources'
3
+
4
+ ActiveAdmin::Resource # autoload
5
+ class ActiveAdmin::Resource
6
+ def resource_table_name
7
+ resource_class.collection_name
8
+ end
9
+ end
10
+
11
+ ActiveAdmin::ResourceController # autoload
12
+ class ActiveAdmin::ResourceController
13
+ # Use #desc and #asc for sorting.
14
+ def sort_order(chain)
15
+ params[:order] ||= active_admin_config.sort_order
16
+ table_name = active_admin_config.resource_table_name
17
+ if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
18
+ chain.send($2, $1)
19
+ else
20
+ chain # just return the chain
21
+ end
22
+ end
23
+
24
+ def search(chain)
25
+ @search = ActiveAdmin::Mongoid::Adaptor::Search.new(chain, clean_search_params(params[:q]))
26
+ end
27
+
28
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveAdmin
2
+ module Mongoid
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'active_admin/mongoid'
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeadmin-mongoidv3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Elia Schito
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '2.4'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activeadmin
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.4'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.4'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sass-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.1'
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: 3.1.4
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ~>
63
+ - !ruby/object:Gem::Version
64
+ version: '3.1'
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 3.1.4
68
+ description: ActiveAdmin hacks to support Mongoid (some ActiveAdmin features are disabled)
69
+ email:
70
+ - elia@schito.me
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - .gitignore
76
+ - Gemfile
77
+ - LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - activeadmin-mongoid.gemspec
81
+ - lib/active_admin-mongoid.rb
82
+ - lib/active_admin/mongoid.rb
83
+ - lib/active_admin/mongoid/adaptor.rb
84
+ - lib/active_admin/mongoid/comments.rb
85
+ - lib/active_admin/mongoid/document.rb
86
+ - lib/active_admin/mongoid/filter_form_builder.rb
87
+ - lib/active_admin/mongoid/resource.rb
88
+ - lib/active_admin/mongoid/version.rb
89
+ - lib/activeadmin-mongoid.rb
90
+ homepage: ''
91
+ licenses:
92
+ - MIT
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.24
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: ActiveAdmin hacks to support Mongoid
115
+ test_files: []