activeadmin_associations 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. data/.coveralls.yml +1 -0
  2. data/.gitignore +57 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +7 -0
  5. data/Gemfile +9 -0
  6. data/MIT_LICENSE.txt +20 -0
  7. data/README.md +220 -0
  8. data/Rakefile +20 -0
  9. data/activeadmin_associations.gemspec +32 -0
  10. data/app/controllers/autocomplete_controller.rb +37 -0
  11. data/app/helpers/active_admin_associations_helper.rb +77 -0
  12. data/app/views/admin/shared/_add_to_association.html.erb +21 -0
  13. data/app/views/admin/shared/_association_collection_table_actions.html.erb +4 -0
  14. data/app/views/admin/shared/_blank_slate.html.erb +3 -0
  15. data/app/views/admin/shared/_collection_table.html.erb +58 -0
  16. data/app/views/admin/shared/_form.html.erb +7 -0
  17. data/config/routes.rb +7 -0
  18. data/lib/active_admin_associations/active_admin_extensions.rb +17 -0
  19. data/lib/active_admin_associations/association_actions.rb +77 -0
  20. data/lib/active_admin_associations/association_config.rb +50 -0
  21. data/lib/active_admin_associations/autocompleter.rb +61 -0
  22. data/lib/active_admin_associations/engine.rb +23 -0
  23. data/lib/active_admin_associations/form_config_dsl.rb +15 -0
  24. data/lib/active_admin_associations/redirect_destroy_actions.rb +7 -0
  25. data/lib/active_admin_associations/version.rb +3 -0
  26. data/lib/activeadmin_associations.rb +20 -0
  27. data/lib/formtastic/inputs/token_input.rb +43 -0
  28. data/lib/formtastic/token_input_default_for_association.rb +19 -0
  29. data/spec/association_config_spec.rb +41 -0
  30. data/spec/autocompleter_spec.rb +58 -0
  31. data/spec/controllers/admin_posts_controller_spec.rb +87 -0
  32. data/spec/controllers/autocomplete_controller_spec.rb +36 -0
  33. data/spec/dummy/README.rdoc +261 -0
  34. data/spec/dummy/Rakefile +7 -0
  35. data/spec/dummy/app/admin/dashboards.rb +33 -0
  36. data/spec/dummy/app/admin/posts.rb +20 -0
  37. data/spec/dummy/app/admin/tags.rb +11 -0
  38. data/spec/dummy/app/assets/javascripts/active_admin.js +8 -0
  39. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  40. data/spec/dummy/app/assets/stylesheets/active_admin.css.scss +6 -0
  41. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  42. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  43. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  44. data/spec/dummy/app/mailers/.gitkeep +0 -0
  45. data/spec/dummy/app/models/.gitkeep +0 -0
  46. data/spec/dummy/app/models/admin_user.rb +10 -0
  47. data/spec/dummy/app/models/post.rb +11 -0
  48. data/spec/dummy/app/models/tag.rb +15 -0
  49. data/spec/dummy/app/models/tagging.rb +7 -0
  50. data/spec/dummy/app/models/user.rb +8 -0
  51. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  52. data/spec/dummy/config.ru +4 -0
  53. data/spec/dummy/config/application.rb +63 -0
  54. data/spec/dummy/config/boot.rb +10 -0
  55. data/spec/dummy/config/database.yml +19 -0
  56. data/spec/dummy/config/environment.rb +5 -0
  57. data/spec/dummy/config/environments/development.rb +37 -0
  58. data/spec/dummy/config/environments/production.rb +67 -0
  59. data/spec/dummy/config/environments/test.rb +37 -0
  60. data/spec/dummy/config/initializers/active_admin.rb +129 -0
  61. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  62. data/spec/dummy/config/initializers/devise.rb +218 -0
  63. data/spec/dummy/config/initializers/inflections.rb +15 -0
  64. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  65. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  66. data/spec/dummy/config/initializers/session_store.rb +8 -0
  67. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  68. data/spec/dummy/config/locales/devise.en.yml +57 -0
  69. data/spec/dummy/config/locales/en.yml +5 -0
  70. data/spec/dummy/config/routes.rb +62 -0
  71. data/spec/dummy/db/.gitignore +1 -0
  72. data/spec/dummy/db/migrate/20120504220404_devise_create_admin_users.rb +52 -0
  73. data/spec/dummy/db/migrate/20120504221534_create_posts.rb +12 -0
  74. data/spec/dummy/db/migrate/20120504221936_create_users.rb +9 -0
  75. data/spec/dummy/db/migrate/20120504222040_create_tags.rb +8 -0
  76. data/spec/dummy/db/migrate/20120504222247_create_taggings.rb +10 -0
  77. data/spec/dummy/db/schema.rb +65 -0
  78. data/spec/dummy/lib/assets/.gitkeep +0 -0
  79. data/spec/dummy/public/404.html +26 -0
  80. data/spec/dummy/public/422.html +26 -0
  81. data/spec/dummy/public/500.html +25 -0
  82. data/spec/dummy/public/favicon.ico +0 -0
  83. data/spec/dummy/script/rails +6 -0
  84. data/spec/dummy/test/unit/admin_user_test.rb +7 -0
  85. data/spec/dummy/test/unit/post_test.rb +7 -0
  86. data/spec/dummy/test/unit/tag_test.rb +7 -0
  87. data/spec/dummy/test/unit/tagging_test.rb +7 -0
  88. data/spec/dummy/test/unit/user_test.rb +7 -0
  89. data/spec/factories/admin_users.rb +9 -0
  90. data/spec/factories/posts.rb +11 -0
  91. data/spec/factories/taggings.rb +6 -0
  92. data/spec/factories/tags.rb +7 -0
  93. data/spec/factories/users.rb +8 -0
  94. data/spec/features/active_admin_associations_spec.rb +94 -0
  95. data/spec/spec_helper.rb +41 -0
  96. data/spec/support/admin_login_controller_helper.rb +7 -0
  97. data/spec/support/admin_login_integration_helper.rb +8 -0
  98. data/vendor/assets/javascripts/active_admin_associations.js +14 -0
  99. data/vendor/assets/javascripts/jquery.tokeninput.js +915 -0
  100. data/vendor/assets/stylesheets/active_admin_associations.css.scss +18 -0
  101. data/vendor/assets/stylesheets/token-input-facebook.css +121 -0
  102. metadata +383 -0
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,57 @@
1
+ # rcov generated
2
+ coverage
3
+ coverage.data
4
+
5
+ # rdoc generated
6
+ rdoc
7
+
8
+ # yard generated
9
+ doc
10
+ .yardoc
11
+
12
+ # bundler
13
+ .bundle
14
+
15
+ # jeweler generated
16
+ pkg
17
+
18
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
19
+ #
20
+ # * Create a file at ~/.gitignore
21
+ # * Include files you want ignored
22
+ # * Run: git config --global core.excludesfile ~/.gitignore
23
+ #
24
+ # After doing this, these files will be ignored in all your git projects,
25
+ # saving you from having to 'pollute' every project you touch with them
26
+ #
27
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
28
+ #
29
+ # For MacOS:
30
+ #
31
+ #.DS_Store
32
+
33
+ # For TextMate
34
+ #*.tmproj
35
+ #tmtags
36
+
37
+ # For emacs:
38
+ #*~
39
+ #\#*
40
+ #.\#*
41
+
42
+ # For vim:
43
+ #*.swp
44
+
45
+ # For redcar:
46
+ #.redcar
47
+
48
+ # For rubinius:
49
+ #*.rbc
50
+
51
+ Gemfile.lock
52
+
53
+ .rvmrc
54
+
55
+ .ruby-version
56
+
57
+ coverage/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+ services: sqlite3
6
+ before_script:
7
+ - bundle exec rake db:migrate
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'meta_search', '>= 1.1.0.pre'
4
+ gem 'kaminari'
5
+ gem 'formtastic', '~> 2.1' # Until activeadmin updates for formatastic 2.1+
6
+ gem 'sass-rails'
7
+ gem 'launchy'
8
+
9
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Brian Landau
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,220 @@
1
+ # ActiveAdmin Associations
2
+
3
+ [![Code Climate](https://codeclimate.com/github/vigetlabs/active_admin_associations.png)](https://codeclimate.com/github/vigetlabs/active_admin_associations)
4
+
5
+ This extends ActiveAdmin to allow for better editing of associations.
6
+
7
+ ## Setup
8
+
9
+ ### Install the gem
10
+
11
+ Add this to your `Gemfile`:
12
+
13
+ gem 'activeadmin_associations'
14
+
15
+ Then run `bundle install`.
16
+
17
+ ### Includes styles we'll need
18
+
19
+ Add `@import "active_admin_associations";` to the top of your `app/assets/stylesheets/active_admin.css.scss`
20
+
21
+ These styles are used by both the autocomplete functionality and the association tables at the bottom of form pages.
22
+
23
+ ### Autocomplete
24
+
25
+ On many applications you end up with large datasets, trying to select an element from those data sets via a select input (Formtastic's default) is less then ideal for a couple reasons. One, it's hard to navigate a large select list. Two, loading all those records into memory to populate the select list can be time consuming and cause the page to load slowly.
26
+
27
+ So I've packaged [jquery-tokeninput](https://github.com/loopj/jquery-tokeninput), an autocomplete results controller, and an ActiveRecord macro together to help improve this.
28
+
29
+ If you aren't interested in using any of this just add this to your `application.rb` config:
30
+
31
+ config.activeadmin_associations.autocomplete = false
32
+
33
+ If you do want it here's how you set it up:
34
+
35
+ #### Setting up autocomplete
36
+
37
+ First, we'll need to make sure the JS and CSS is setup for the admin part of the site.
38
+
39
+ * Add `//= require active_admin_associations` to the top of your `app/assets/javascripts/active_admin.js` file.
40
+ * Add `autocomplete` statements to models you want to be able to autocomplete in the admin.
41
+ * This first parameter it takes is a column/attribute name like `:title`.
42
+ * The second parameter is an options hash which for now only uses 1 value `:format_label`
43
+ Format Label isn't needed for jquery.tokeninput.js but it is useful when using jQueryUI's autocomplete in other parts of your site. It can allow you to custom format the display label for the autocomplete results displayed by jQueryUI.
44
+ The `:format_label` option should be either a symbol that is a name of a method on an instance of the model, or a proc (or anything that responds to call) that takes 1 parameter which will be the record.
45
+ Example:
46
+ <code><pre>
47
+ autocomplete :name, :format_label => proc {|speaker|
48
+ label = "<span id=\"speaker-#{speaker.id}\">#{speaker.name} <em>("
49
+ label << "#{speaker.position}, " unless speaker.position.blank?
50
+ label << "#{speaker.talk_count} talk#{'s' unless speaker.talk_count == 1})</em></span>"
51
+ label
52
+ }
53
+ </pre></code>
54
+ * Set values for `config.activeadmin_associations.autocomplete_models` in your `config/application.rb`. This should be a list of the models that you have added `autocomplete` statements to:
55
+
56
+ `config.activeadmin_associations.autocomplete_models = %w(post user tag)`
57
+
58
+ If you plan to use other autocomplete JS libraries there are 2 other configs you may want to look at:
59
+
60
+ Different libraries send different param names for the query to the autocomplete endpoint you give it. For instance, jquery.tokeninput uses the `q` parameter while jQueryUI uses the `term` parameter. If no setting is given we will just use the `q` parameter. To configure this you need a statement like this in your `config/application.rb`:
61
+
62
+ config.activeadmin_associations.autocomplete_query_term_param_names = [:q, :term]
63
+
64
+ It might happen that the hash the autocomplete ActiveRecord macro provides for individual results won't play nice with the JS autocomplete plugin you're using. In this case we provide a way to format individual results yourself. Just assign an object that responds to call (like a proc) to `config.activeadmin_associations.autocomplete_result_formatter` in your `config/application.rb` like so:
65
+
66
+ config.activeadmin_associations.autocomplete_result_formatter = proc { |record, autocomplete_attribute, autocomplete_options|
67
+ {:name => record.send(autocomplete_attribute), :id => record.id,
68
+ :another_value => record.send(autocomplete_options[:other_value_method])}
69
+ }
70
+
71
+
72
+ ### Other Configuration
73
+
74
+ We add functionality so that when you do a destroy action you are redirected back to the Referer or the ActiveAdmin Dashboard. If you'd like to remove this functionality you can just put this in your `config/application.rb`:
75
+
76
+ config.activeadmin_associations.destroy_redirect = false
77
+
78
+
79
+ ### Setup your admin resource definitions
80
+
81
+ The main thing this Rails Engine provides is a way to easily configure simple forms that handle `has_many` relationships better then how ActiveAdmin does out of the box. Since we don't override any core ActiveAdmin functionality you can include this in resources you want to use it on and not on others.
82
+
83
+ #### Here's how you get started:
84
+
85
+ Add `association_actions` somewhere inside your ActiveAdmin resource definition block:
86
+
87
+ ActiveAdmin.register Post do
88
+ association_actions
89
+ # ...
90
+ end
91
+
92
+ You then also need to tell it you want to use the form template bundled with this Engine:
93
+
94
+ ActiveAdmin.register Post do
95
+ association_actions
96
+
97
+ form :partial => "admin/shared/form"
98
+ # ...
99
+ end
100
+
101
+ Now you need to define the columns and the `has_many` relationships:
102
+
103
+ ActiveAdmin.register Post do
104
+ association_actions
105
+
106
+ form :partial => "admin/shared/form"
107
+
108
+ form_columns :title, :body, :slug, :author, :published_at, :featured
109
+
110
+ form_associations do
111
+ association :tags, [:name, :post_count]
112
+ association :revisions do
113
+ fields :version_number, :created_at, :update_at
114
+ end
115
+ end
116
+ end
117
+
118
+ #### Defining the columns you want to edit in your form:
119
+
120
+ Pass to the `form_columns` method a list of column that there should be inputs for on the form.
121
+
122
+
123
+ #### Defining associations to manage at the bottom of edit pages:
124
+
125
+ The `form_associations` is used to define the associations you want to manage at the bottom of the edit pages. This method takes a block that is used to define these associations and the columns to display.
126
+
127
+ If you use the `associations` method inside the block then you can define multiple associations at once:
128
+
129
+ form_associations do
130
+ association :tags, :revisions
131
+ end
132
+
133
+ In this case all the `content_columns` for the models will be used as the columns in the association tables. This is probably good for getting started quickly but you'll probably find you quickly outgrow it.
134
+
135
+ You can use individual `association` method calls and pass the list of attributes/methods to use as columns in the table:
136
+
137
+ form_associations do
138
+ association :tags, [:name, :post_count]
139
+ end
140
+
141
+ You can also define the columns inside a block passed to the `association` method with a call to the `fields` method:
142
+
143
+ form_associations do
144
+ association :revisions do
145
+ fields :version_number, :created_at, :update_at
146
+ end
147
+ end
148
+
149
+ Or if you prefer you can use multiple calls to the `field` method:
150
+
151
+ form_associations do
152
+ association :revisions do
153
+ field :version_number
154
+ field :created_at
155
+ field :update_at
156
+ end
157
+ end
158
+
159
+ You are also free to mix and match:
160
+
161
+ form_associations do
162
+ association :revisions, [:version_number] do
163
+ fields :created_at, :another_column
164
+ field :update_at
165
+ end
166
+ end
167
+
168
+
169
+ #### Fine grained control over the form:
170
+
171
+ If you want more control over the main part of the form you can define a `active_association_form` which takes a block with 1 parameter (which is the form object):
172
+
173
+ ActiveAdmin.register Post do
174
+ association_actions
175
+
176
+ form :partial => "admin/shared/form"
177
+
178
+ active_association_form do |f|
179
+ f.inputs do
180
+ f.input :title
181
+ f.input :body
182
+ f.input :slug, :label => "This is the value that will be used in the URL bar for the post."
183
+ end
184
+ f.inputs do
185
+ f.input :author, :as => :select
186
+ f.input :published_at
187
+ end
188
+ end
189
+
190
+ form_associations do
191
+ association :tags, [:name, :post_count]
192
+ association :revisions do
193
+ fields :version_number, :created_at, :update_at
194
+ end
195
+ end
196
+ end
197
+
198
+ #### Overriding the templates
199
+
200
+ If this still doesn't give you the power you're looking for you can override any of the partial templates this engine uses.
201
+
202
+ * `admin/shared/_form.html.erb` – you probably don't want to override this one instead you probably want to use your own `_form.html.erb` template in your `app/views/admin/RESOURCE_NAME` directory and have this in your AA resource config: `form :partial => 'form'`. But if you really want to change how all the activeadmin_associations forms look you can.
203
+ * `admin/shared/_collection_tabe.html.erb` – this is how we generate the tables for the `has_many` relationships below the form. Once again not something I'd recommend editing.
204
+ * `admin/shared/_association_collection_table_actions.html.erb` – this defines the actions that you can do on each related record. The default is "edit" and "unrelate". You may want to override this for instance to define different actions for different models.
205
+ * `admin/shared/_add_to_association.html.erb` – This is the form to relate existing records to the parent record.
206
+
207
+
208
+ ## Contributing to ActiveAdmin Associations
209
+
210
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
211
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
212
+ * Fork the project.
213
+ * Start a feature/bugfix branch.
214
+ * Commit and push until you are happy with your contribution.
215
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
216
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
217
+
218
+ ## Copyright
219
+
220
+ Copyright (c) 2012 Brian Landau (Viget). See MIT_LICENSE.txt for further details.
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
9
+ load 'rails/tasks/engine.rake'
10
+
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ require 'rspec/core'
14
+ require 'rspec/core/rake_task'
15
+
16
+ desc "Run all specs in spec directory"
17
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
18
+
19
+ task :default => :spec
20
+
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "active_admin_associations/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "activeadmin_associations"
7
+ s.version = ActiveAdminAssociations::VERSION
8
+ s.authors = ["Brian Landau"]
9
+ s.email = ["brian.landau@viget.com"]
10
+ s.homepage = "http://github.com/vigetlabs/active_admin_associations"
11
+ s.summary = %q{This extends ActiveAdmin to allow for better editing of associations.}
12
+ s.description = %q{This extends ActiveAdmin to allow for better editing of associations.}
13
+ s.date = '2012-05-03'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ s.extra_rdoc_files = ["README.md", "MIT_LICENSE.txt"]
20
+
21
+ s.add_dependency 'activeadmin', '~> 0.6.0'
22
+ s.add_dependency 'rails', '~> 3.2'
23
+
24
+ s.add_development_dependency 'rspec-rails'
25
+ s.add_development_dependency 'shoulda-matchers', '~> 1.5'
26
+ s.add_development_dependency 'capybara', '~> 2.2'
27
+ s.add_development_dependency 'sqlite3'
28
+ s.add_development_dependency 'bundler', '~> 1.0'
29
+ s.add_development_dependency 'database_cleaner'
30
+ s.add_development_dependency 'factory_girl_rails', '~> 1.7'
31
+ s.add_development_dependency 'coveralls'
32
+ end
@@ -0,0 +1,37 @@
1
+ class AutocompleteController < ApplicationController
2
+ def index
3
+ respond_to do |format|
4
+ format.json {
5
+ render :json => autocomplete_results
6
+ }
7
+ end
8
+ end
9
+
10
+ private
11
+
12
+ def autocomplete_results
13
+ query_term.present? ? model.autocomplete_results(query_term) : []
14
+ end
15
+
16
+ def model
17
+ params[:model].classify.constantize
18
+ end
19
+
20
+ def query_param_name
21
+ if activeadmin_associations_config.autocomplete_query_term_param_names.present?
22
+ activeadmin_associations_config.autocomplete_query_term_param_names.detect do |param_name|
23
+ params.keys.map(&:to_sym).include?(param_name.to_sym)
24
+ end
25
+ else
26
+ :q
27
+ end
28
+ end
29
+
30
+ def query_term
31
+ params[query_param_name]
32
+ end
33
+
34
+ def activeadmin_associations_config
35
+ Rails.application.config.activeadmin_associations
36
+ end
37
+ end
@@ -0,0 +1,77 @@
1
+ module ActiveAdminAssociationsHelper
2
+ def collection_relationship_manager(object, association)
3
+ collection = object.send(association.name).page(1)
4
+ relationship_class = object.class.reflect_on_association(association.name).klass
5
+ columns = association.fields.presence || relationship_class.content_columns
6
+ render :partial => 'admin/shared/collection_table', :locals => {
7
+ :object => object,
8
+ :collection => collection,
9
+ :relationship => association.name,
10
+ :columns => columns,
11
+ :relationship_class => relationship_class
12
+ }
13
+ end
14
+
15
+ def admin_form_for(record)
16
+ active_admin_form_for [:admin, record] do |f|
17
+ f.semantic_errors
18
+ if active_admin_config.form_columns.present?
19
+ f.inputs *active_admin_config.form_columns
20
+ end
21
+ if active_admin_config.active_association_form && active_admin_config.active_association_form.respond_to?(:call)
22
+ active_admin_config.active_association_form.call(f)
23
+ end
24
+ f.actions
25
+ end
26
+ end
27
+
28
+ def edit_url_for(record)
29
+ send("edit_admin_#{record.class.model_name.singular}_path", record)
30
+ end
31
+
32
+ def display_method_name_for(record)
33
+ Formtastic::FormBuilder.collection_label_methods.find { |m| record.respond_to?(m) }
34
+ end
35
+
36
+ def display_name_for(record)
37
+ record.send(display_method_name_for(record))
38
+ end
39
+
40
+ def resource_administrated?(model_class)
41
+ ActiveAdmin.resources.include?(model_class)
42
+ end
43
+
44
+ def relate_to_url(object)
45
+ send("relate_admin_#{object.class.model_name.singular}_path", object)
46
+ end
47
+
48
+ def page_entries_info(collection, options = {})
49
+ if options[:entry_name]
50
+ entry_name = options[:entry_name]
51
+ entries_name = options[:entries_name]
52
+ elsif collection.empty?
53
+ entry_name = I18n.translate("active_admin.pagination.entry", :count => 1, :default => 'entry')
54
+ entries_name = I18n.translate("active_admin.pagination.entry", :count => 2, :default => 'entries')
55
+ else
56
+ begin
57
+ entry_name = I18n.translate!("activerecord.models.#{collection.first.class.model_name.i18n_key}", :count => 1)
58
+ entries_name = I18n.translate!("activerecord.models.#{collection.first.class.model_name.i18n_key}", :count => collection.size)
59
+ rescue I18n::MissingTranslationData
60
+ entry_name = collection.first.class.name.underscore.sub('_', ' ')
61
+ end
62
+ end
63
+ entries_name = entry_name.pluralize unless entries_name
64
+
65
+ if collection.num_pages < 2
66
+ case collection.size
67
+ when 0; I18n.t('active_admin.pagination.empty', :model => entries_name)
68
+ when 1; I18n.t('active_admin.pagination.one', :model => entry_name)
69
+ else; I18n.t('active_admin.pagination.one_page', :model => entries_name, :n => collection.total_count)
70
+ end
71
+ else
72
+ offset = collection.current_page * collection.size
73
+ total = collection.total_count
74
+ I18n.t('active_admin.pagination.multiple', :model => entries_name, :from => (offset - collection.size + 1), :to => offset > total ? total : offset, :total => total)
75
+ end
76
+ end
77
+ end