From 5c3c98f10fb3178b6cfafd8244e6b4dbdff68a32 Mon Sep 17 00:00:00 2001 From: Brian Landau Date: Fri, 4 May 2012 14:05:06 -0600 Subject: [PATCH] Initial commit --- .gitignore | 51 + Gemfile | 3 + LICENSE.txt | 20 + README.md | 165 ++++ Rakefile | 10 + aa_associations.gemspec | 26 + app/controllers/autocomplete_controller.rb | 33 + .../active_admin_associations_helper.rb | 75 ++ ...ociation_collection_table_actions.html.erb | 5 + app/views/admin/shared/_blank_slate.html.erb | 3 + .../admin/shared/_collection_table.html.erb | 72 ++ app/views/admin/shared/_form.html.erb | 7 + config/routes.rb | 7 + lib/aa_associations.rb | 19 + .../active_admin_extensions.rb | 13 + .../association_actions.rb | 40 + .../autocompleter.rb | 64 ++ lib/active_admin_associations/engine.rb | 23 + .../form_config_dsl.rb | 15 + .../redirect_destroy_actions.rb | 7 + lib/active_admin_associations/version.rb | 3 + lib/formtastic/inputs/token_input.rb | 43 + .../token_input_default_for_association.rb | 19 + test/helper.rb | 18 + test/test_active_form_config.rb | 7 + .../javascripts/active_admin_associations.js | 14 + .../assets/javascripts/jquery.tokeninput.js | 915 ++++++++++++++++++ .../active_admin_associations.css.scss | 18 + .../stylesheets/token-input-facebook.css | 121 +++ 29 files changed, 1816 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 aa_associations.gemspec create mode 100644 app/controllers/autocomplete_controller.rb create mode 100644 app/helpers/active_admin_associations_helper.rb create mode 100644 app/views/admin/shared/_association_collection_table_actions.html.erb create mode 100644 app/views/admin/shared/_blank_slate.html.erb create mode 100644 app/views/admin/shared/_collection_table.html.erb create mode 100644 app/views/admin/shared/_form.html.erb create mode 100644 config/routes.rb create mode 100644 lib/aa_associations.rb create mode 100644 lib/active_admin_associations/active_admin_extensions.rb create mode 100644 lib/active_admin_associations/association_actions.rb create mode 100644 lib/active_admin_associations/autocompleter.rb create mode 100644 lib/active_admin_associations/engine.rb create mode 100644 lib/active_admin_associations/form_config_dsl.rb create mode 100644 lib/active_admin_associations/redirect_destroy_actions.rb create mode 100644 lib/active_admin_associations/version.rb create mode 100644 lib/formtastic/inputs/token_input.rb create mode 100644 lib/formtastic/token_input_default_for_association.rb create mode 100644 test/helper.rb create mode 100644 test/test_active_form_config.rb create mode 100644 vendor/assets/javascripts/active_admin_associations.js create mode 100644 vendor/assets/javascripts/jquery.tokeninput.js create mode 100644 vendor/assets/stylesheets/active_admin_associations.css.scss create mode 100644 vendor/assets/stylesheets/token-input-facebook.css diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48634e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,51 @@ +# rcov generated +coverage +coverage.data + +# rdoc generated +rdoc + +# yard generated +doc +.yardoc + +# bundler +.bundle + +# jeweler generated +pkg + +# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore: +# +# * Create a file at ~/.gitignore +# * Include files you want ignored +# * Run: git config --global core.excludesfile ~/.gitignore +# +# After doing this, these files will be ignored in all your git projects, +# saving you from having to 'pollute' every project you touch with them +# +# 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) +# +# For MacOS: +# +#.DS_Store + +# For TextMate +#*.tmproj +#tmtags + +# For emacs: +#*~ +#\#* +#.\#* + +# For vim: +#*.swp + +# For redcar: +#.redcar + +# For rubinius: +#*.rbc + +Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..c80ee36 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source "http://rubygems.org" + +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..0f65f08 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,20 @@ +Copyright (c) 2012 Brian Landau + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9b03865 --- /dev/null +++ b/README.md @@ -0,0 +1,165 @@ +# ActiveAdmin Associations + +TODO: Description + +## Setup + +### Install the gem + +Add this to your `Gemfile`: + + gem 'aa_associations' + +Then run `bundle install`. + + +### Autocomplete + +On many applications you end up with large datasets, try 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. + +So I've packaged [jquery-tokeninput](https://github.com/loopj/jquery-tokeninput), an autocomplete results controller, and an ActiveRecord macro. + +If you aren't interested in using any of this just add this to your `application.rb` config: + + config.aa_associations.autocomplete = false + +If you do want it here's how you set it up: + +#### Setting up autocomplete + +First, we'll need to make sure the JS and CSS is setup for the admin part of the site. + +* Add `//= require active_admin_associations` to the top of your `app/assets/javascripts/active_admin.js` file. +* Add `@import "active_admin_associations";` to the top of your `app/assets/stylesheets/active_admin.css.scss` +* Add `autocomplete` statements to models you want to be able to autocomplete in the admin. + * This first parameter it takes is a column/attribute name like `:title`. + * The second parameter is an options has which for now only uses 1 value `:format_label` + 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. + 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. + Example: + ```ruby + autocomplete :name, :format_label => proc {|speaker| + label = "#{speaker.name} (" + label << "#{speaker.position}, " unless speaker.position.blank? + label << "#{speaker.talk_count} talk#{'s' unless speaker.talk_count == 1})" + label + } + ``` +* Set values for `config.aa_associations.autocomplete_models` in your `config/application.rb`. This should be a list of the models that you have added `autocomplete` statements to: + + `config.aa_associations.autocomplete_models = %w(post user tag)` + +If you plan to use other autocomplete JS libraries there are 2 other configs you may want to look at: + +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`: + + config.aa_associations.autocomplete_query_term_param_names = [:q, :term] + +It might happen that the hash the autocomplete formatter provides for individual results won't play nice with the JS autocomplete plugin your 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.aa_associations.autocomplete_result_formatter` in your `config/application.rb` like so: + + config.aa_associations.autocomplete_result_formatter = proc { |record, autocomplete_attribute, autocomplete_options| + {:name => record.send(autocomplete_attribute), :id => record.id, + :another_value => record.send(autocomplete_options[:other_value_method])} + } + + +### Other Configuration + +We add functionality so that when you do a destroy action you are redirect 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`: + + config.aa_associations.destroy_redirect = false + + +### Setup your admin resource definitions + +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. + +#### Here's how you get started: + +Add `association_actions` somewhere inside your ActiveAdmin resource definition block: + + ActiveAdmin.register Post do + association_actions + # ... + end + +You then also need to tell it you want to use the form template bundled with this Engine: + + ActiveAdmin.register Post do + association_actions + + form :partial => "admin/shared/form" + # ... + end + +Now you need to define the columns and the `has_many` relationships: + + ActiveAdmin.register Post do + association_actions + + form :partial => "admin/shared/form" + + form_columns [:title, :body, :slug, :author, :published_at, :featured] + + form_relationships [ + [:tags, [:name, :post_count]], + [:revisions, [:version_number, :created_at, :update_at]] + ] + end + +* `form_columns` is an array of attributes on your model that you want to create form inputs for. +* `form_relationships` is an array of arrays that define the relationships you want tables for and the columns you want displayed for each relationship. + +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): + + ActiveAdmin.register Post do + association_actions + + form :partial => "admin/shared/form" + + active_association_form do |f| + f.inputs do + f.input :title + f.input :body + f.input :slug, :label => "This is the value that will be used in the URL bar for the post." + end + f.inputs do + f.input :author, :as => :select + f.input :published_at + end + end + + form_relationships [ + [:tags, [:name, :post_count]], + [:revisions, [:version_number, :created_at, :update_at]] + ] + end + +#### Overriding the templates + +If this still doesn't give you the power you're looking for you can override any of the partial templates this engine uses. + +* `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 aa_associations forms look you can. +* `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 +* `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. + + +## TODO + +* Break up views into more partials +* Improve `form_relationships` API + + +## Contributing to ActiveAdmin Associations + +* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. +* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. +* Fork the project. +* Start a feature/bugfix branch. +* Commit and push until you are happy with your contribution. +* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. +* 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. + +## Copyright + +Copyright (c) 2012 Brian Landau (Viget). See LICENSE.txt for further details. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..6ea8769 --- /dev/null +++ b/Rakefile @@ -0,0 +1,10 @@ +# encoding: utf-8 + +require 'rake/testtask' +Rake::TestTask.new(:test) do |test| + test.libs << 'lib' << 'test' + test.pattern = 'test/**/*_test.rb' + test.verbose = true +end + +task :default => :test diff --git a/aa_associations.gemspec b/aa_associations.gemspec new file mode 100644 index 0000000..c467d30 --- /dev/null +++ b/aa_associations.gemspec @@ -0,0 +1,26 @@ +# -*- encoding: utf-8 -*- +$:.push File.expand_path("../lib", __FILE__) +require "active_admin_associations/version" + +Gem::Specification.new do |s| + s.name = "aa_associations" + s.version = ActiveAdminAssociations::VERSION + s.authors = ["Brian Landau"] + s.email = ["brian.landau@viget.com"] + s.homepage = "http://github.com/vigetlabs/active_admin_associations" + s.summary = %q{TODO} + s.description = %q{TODO} + s.date = '2012-05-03' + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] + s.extra_rdoc_files = ["README.md"] + + s.add_dependency 'activeadmin', '~> 0.4' + s.add_dependency 'rails', '~> 3.1' + + s.add_development_dependency 'shoulda' + s.add_development_dependency 'bundler', '~> 1.0' +end diff --git a/app/controllers/autocomplete_controller.rb b/app/controllers/autocomplete_controller.rb new file mode 100644 index 0000000..52dc0ac --- /dev/null +++ b/app/controllers/autocomplete_controller.rb @@ -0,0 +1,33 @@ +class AutocompleteController < ApplicationController + def index + respond_to do |format| + format.json { + render :json => model.autocomplete_results(query_term) + } + end + end + + private + + def model + params[:model].classify.constantize + end + + def query_param_name + if aa_associations_config.autocomplete_query_term_param_names.present? + aa_associations_config.autocomplete_query_term_param_names.detect do |param_name| + params.keys.map(&:to_sym).include?(param_name.to_sym) + end + else + :q + end + end + + def query_term + params[query_param_name] + end + + def aa_associations_config + Rails.application.config.aa_associations + end +end diff --git a/app/helpers/active_admin_associations_helper.rb b/app/helpers/active_admin_associations_helper.rb new file mode 100644 index 0000000..4e0e8a1 --- /dev/null +++ b/app/helpers/active_admin_associations_helper.rb @@ -0,0 +1,75 @@ +module ActiveAdminAssociationsHelper + def collection_relationship_manager(object, relationship_name, columns) + collection = object.send(relationship_name).page(1) + render :partial => 'admin/shared/collection_table', :locals => { + :object => object, + :collection => collection, + :relationship => relationship_name, + :columns => columns, + :relationship_class => object.class.reflect_on_association(relationship_name).klass + } + end + + def admin_form_for(record) + active_admin_form_for [:admin, record] do |f| + f.semantic_errors + if active_admin_config.form_columns.present? + f.inputs *active_admin_config.form_columns + end + if active_admin_config.active_association_form && active_admin_config.active_association_form.respond_to?(:call) + active_admin_config.active_association_form.call(f) + end + f.buttons + end + end + + def edit_url_for(record) + send("edit_admin_#{record.class.model_name.singular}_path", record) + end + + def display_method_name_for(record) + Formtastic::FormBuilder.collection_label_methods.find { |m| record.respond_to?(m) } + end + + def display_name_for(record) + record.send(display_method_name_for(record)) + end + + def resource_administrated?(model_class) + ActiveAdmin.resources.include?(model_class) + end + + def relate_to_url(object) + send("relate_admin_#{object.class.model_name.singular}_path", object) + end + + def page_entries_info(collection, options = {}) + if options[:entry_name] + entry_name = options[:entry_name] + entries_name = options[:entries_name] + elsif collection.empty? + entry_name = I18n.translate("active_admin.pagination.entry", :count => 1, :default => 'entry') + entries_name = I18n.translate("active_admin.pagination.entry", :count => 2, :default => 'entries') + else + begin + entry_name = I18n.translate!("activerecord.models.#{collection.first.class.model_name.i18n_key}", :count => 1) + entries_name = I18n.translate!("activerecord.models.#{collection.first.class.model_name.i18n_key}", :count => collection.size) + rescue I18n::MissingTranslationData + entry_name = collection.first.class.name.underscore.sub('_', ' ') + end + end + entries_name = entry_name.pluralize unless entries_name + + if collection.num_pages < 2 + case collection.size + when 0; I18n.t('active_admin.pagination.empty', :model => entries_name) + when 1; I18n.t('active_admin.pagination.one', :model => entry_name) + else; I18n.t('active_admin.pagination.one_page', :model => entries_name, :n => collection.total_count) + end + else + offset = collection.current_page * collection.size + total = collection.total_count + I18n.t('active_admin.pagination.multiple', :model => entries_name, :from => (offset - collection.size + 1), :to => offset > total ? total : offset, :total => total) + end + end +end diff --git a/app/views/admin/shared/_association_collection_table_actions.html.erb b/app/views/admin/shared/_association_collection_table_actions.html.erb new file mode 100644 index 0000000..e1c58e5 --- /dev/null +++ b/app/views/admin/shared/_association_collection_table_actions.html.erb @@ -0,0 +1,5 @@ +<%= link_to "Edit", edit_url_for(record) %> +<%= button_to "Unrelate", {:action => :unrelate, :id => object.to_param, + :relationship_name => relationship, + :related_id => record.id}, :method => :put %> +<%- end -%> diff --git a/app/views/admin/shared/_blank_slate.html.erb b/app/views/admin/shared/_blank_slate.html.erb new file mode 100644 index 0000000..d69bbe2 --- /dev/null +++ b/app/views/admin/shared/_blank_slate.html.erb @@ -0,0 +1,3 @@ +
+ <%= blank_text %> +
diff --git a/app/views/admin/shared/_collection_table.html.erb b/app/views/admin/shared/_collection_table.html.erb new file mode 100644 index 0000000..0fd4a0c --- /dev/null +++ b/app/views/admin/shared/_collection_table.html.erb @@ -0,0 +1,72 @@ +
+

<%= object.class.human_attribute_name(relationship) %>

+ <%- if resource_administrated?(relationship_class) && relationship_class.respond_to?(:autocomplete_attribute) && relationship_class.autocomplete_attribute.present? -%> + <%= form_tag relate_to_url(object), :class => 'relate-to-form', :method => :put do %> +
+

Add a <%= relationship_class.model_name.human %>

+ <%= hidden_field_tag 'relationship_name', relationship %> +
    +
  1. + +
  2. +
  3. <%= hidden_field_tag 'related_id', nil, :class => 'token-input', + 'data-model-name' => relationship_class.model_name.singular %>
  4. +
  5. <%= submit_tag "Add #{relationship_class.model_name.human}" %>
  6. +
+
+ <% end %> + <%- end -%> + <%- if collection.present? -%> + + + + + <% columns.each do |column| %> + + <%- end -%> + + + + + <%- collection.each do |record| -%> + "> + + <%- columns.each do |column| -%> + <%- if record.send(column).is_a?(ActiveRecord::Base) -%> + + <%- else -%> + + <%- end -%> + <%- end -%> + + + <%- end -%> + +
ID<%= relationship_class.human_attribute_name(column) %> 
<%= record.id %><%= display_name_for(record.send(column)) %><%= record.send(column) %> + <%- if resource_administrated?(record.class) -%> + <%= render :partial => 'admin/shared/association_collection_table_actions', :locals => { + :record => record, + :object => object, + :relationship => relationship + } %> + <%- else -%> +   + <%- end -%> +
+ + <%- else -%> + <%= render :partial => 'admin/shared/blank_slate', :locals => { + :blank_text => "There are no #{object.class.human_attribute_name(relationship)}" + } %> + <%- end -%> +
diff --git a/app/views/admin/shared/_form.html.erb b/app/views/admin/shared/_form.html.erb new file mode 100644 index 0000000..0b80b05 --- /dev/null +++ b/app/views/admin/shared/_form.html.erb @@ -0,0 +1,7 @@ +<%= admin_form_for resource %> + +<%- if active_admin_config.form_relationships.present? -%> + <%- active_admin_config.form_relationships.each do |relationship, columns| -%> + <%= collection_relationship_manager(resource, relationship, columns) %> + <%- end -%> +<%- end -%> diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..4159ed6 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,7 @@ +if Rails.application.config.aa_associations.autocomplete_models + models = Rails.application.config.aa_associations.autocomplete_models.join('|') + Rails.application.routes.draw do + match '/autocomplete/:model' => 'autocomplete#index', :model => /(#{models})/, + :defaults => { :format => 'json' } + end +end diff --git a/lib/aa_associations.rb b/lib/aa_associations.rb new file mode 100644 index 0000000..822bcca --- /dev/null +++ b/lib/aa_associations.rb @@ -0,0 +1,19 @@ +require 'active_support' +require 'activeadmin' +require 'active_admin_associations/version' +require 'active_admin_associations/active_admin_extensions' +require 'formtastic/token_input_default_for_association' +require 'formtastic/inputs/token_input' + +module ActiveAdminAssociations + extend ActiveSupport::Autoload + + eager_autoload do + autoload :AssociationActions + autoload :FormConfigDSL + autoload :RedirectDestroyActions + autoload :Autocompleter + end +end + +require 'active_admin_associations/engine' diff --git a/lib/active_admin_associations/active_admin_extensions.rb b/lib/active_admin_associations/active_admin_extensions.rb new file mode 100644 index 0000000..f4c5ae5 --- /dev/null +++ b/lib/active_admin_associations/active_admin_extensions.rb @@ -0,0 +1,13 @@ +module ActiveAdmin + class Resource + attr_accessor :form_columns + attr_accessor :form_relationships + attr_accessor :active_association_form + end + + class << self + def resources + application.namespaces.values.map{|n| n.resources.resources }.flatten.compact.map(&:resource_class) + end + end +end diff --git a/lib/active_admin_associations/association_actions.rb b/lib/active_admin_associations/association_actions.rb new file mode 100644 index 0000000..18829e4 --- /dev/null +++ b/lib/active_admin_associations/association_actions.rb @@ -0,0 +1,40 @@ +module ActiveAdminAssociations + module AssociationActions + def association_actions + member_action :unrelate, :method => :put do + reflection = resource_class.reflect_on_association(params[:relationship_name].to_sym) + if reflection.collection? + related_record = reflection.klass.find(params[:related_id]) + resource.send(params[:relationship_name]).delete(related_record) + else + resource.update_attribute("#{params[:relationship_name]}_id", nil) + end + flash[:notice] = "The recored has been unrelated." + redirect_to request.headers["Referer"].presence || admin_dashboard_url + end + + member_action :relate, :method => :put do + reflection = resource_class.reflect_on_association(params[:relationship_name].to_sym) + if reflection.collection? + record_to_relate = reflection.klass.find(params[:related_id]) + resource.send(params[:relationship_name]) << record_to_relate + else + resource.update_attribute("#{params[:relationship_name]}_id", record_to_relate) + end + flash[:notice] = "The recored has been related." + redirect_to request.headers["Referer"].presence || admin_dashboard_url + end + + member_action :page_related, :method => :get do + relationship_name = params[:relationship_name].to_sym + render :partial => 'admin/shared/collection_table', :locals => { + :object => resource, + :collection => resource.send(relationship_name).page(params[:page]), + :relationship => relationship_name, + :columns => active_admin_config.form_relationships[relationship_name], + :relationship_class => resource_class.reflect_on_association(relationship_name).klass + } + end + end + end +end \ No newline at end of file diff --git a/lib/active_admin_associations/autocompleter.rb b/lib/active_admin_associations/autocompleter.rb new file mode 100644 index 0000000..6516644 --- /dev/null +++ b/lib/active_admin_associations/autocompleter.rb @@ -0,0 +1,64 @@ +module ActiveAdminAssociations + module Autocompleter + extend ActiveSupport::Concern + + module ClassMethods + def autocomplete(attribute, options = {}) + class_attribute :autocomplete_attribute + class_attribute :autocomplete_options + + self.autocomplete_attribute = attribute + self.autocomplete_options = options + + extend AutocompleteMethods + end + end + + module AutocompleteMethods + def autocomplete_results(query) + results = where("LOWER(#{table_name}.#{autocomplete_attribute}) LIKE ?", "#{query.downcase}%"). + order("#{table_name}.#{autocomplete_attribute} ASC") + results.map do |record| + _autocomplete_format_result(record) + end + end + + private + + def _autocomplete_format_result(record) + if configured_autocomplete_result_formatter? + aa_associations_config.autocomplete_result_formatter.call(record, + autocomplete_attribute, autocomplete_options) + else + label = _format_autocomplete_label(record) + {"label" => label, # This plays nice with both jQuery UI autocomplete and jquery.tokeninput + "value" => record.send(autocomplete_attribute), + "id" => record.id} + end + end + + def _format_autocomplete_label(record) + if autocomplete_options[:format_label].present? + if autocomplete_options[:format_label].is_a?(Symbol) + record.send(autocomplete_options[:format_label]) + elsif autocomplete_options[:format_label].respond_to?(:call) + autocomplete_options[:format_label].call(record) + else + record.send(autocomplete_attribute) + end + else + record.send(autocomplete_attribute) + end + end + + def configured_autocomplete_result_formatter? + aa_associations_config.autocomplete_result_formatter.present? && + aa_associations_config.autocomplete_result_formatter.respond_to?(:call) + end + + def aa_associations_config + Rails.application.config.aa_associations + end + end + end +end diff --git a/lib/active_admin_associations/engine.rb b/lib/active_admin_associations/engine.rb new file mode 100644 index 0000000..c9f53c5 --- /dev/null +++ b/lib/active_admin_associations/engine.rb @@ -0,0 +1,23 @@ +module ActiveAdminAssociations + class Engine < Rails::Engine + config.aa_associations = ActiveSupport::OrderedOptions.new + + initializer "active_admin_associations.load_extensions" do |app| + ActiveAdmin::BaseController.helper ActiveAdminAssociationsHelper + ActiveAdmin::ResourceDSL.send(:include, ActiveAdminAssociations::AssociationActions) + ActiveAdmin::ResourceDSL.send(:include, ActiveAdminAssociations::FormConfigDSL) + + unless app.config.aa_associations.destroy_redirect == false + ActiveAdmin::BaseController.send(:include, ActiveAdminAssociations::RedirectDestroyActions) + end + + unless app.config.aa_associations.autocomplete == false + ActiveSupport.on_load(:active_record) do + ActiveRecord::Base.send(:include, ActiveAdminAssociations::Autocompleter) + end + + Formtastic::Helpers::InputHelper.send(:include, Formtastic::TokenInputDefaultForAssociation) + end + end + end +end \ No newline at end of file diff --git a/lib/active_admin_associations/form_config_dsl.rb b/lib/active_admin_associations/form_config_dsl.rb new file mode 100644 index 0000000..7d97616 --- /dev/null +++ b/lib/active_admin_associations/form_config_dsl.rb @@ -0,0 +1,15 @@ +module ActiveAdminAssociations + module FormConfigDSL + def active_association_form(&block) + config.active_association_form = block + end + + def form_columns(column_names) + config.form_columns = column_names + end + + def form_relationships(relations) + config.form_relationships = ActiveSupport::OrderedHash[relations] + end + end +end \ No newline at end of file diff --git a/lib/active_admin_associations/redirect_destroy_actions.rb b/lib/active_admin_associations/redirect_destroy_actions.rb new file mode 100644 index 0000000..4297b79 --- /dev/null +++ b/lib/active_admin_associations/redirect_destroy_actions.rb @@ -0,0 +1,7 @@ +module ActiveAdminAssociations + module RedirectDestroyActions + def destroy + destroy!{ request.headers["Referer"].presence || admin_dashboard_url } + end + end +end diff --git a/lib/active_admin_associations/version.rb b/lib/active_admin_associations/version.rb new file mode 100644 index 0000000..94414cc --- /dev/null +++ b/lib/active_admin_associations/version.rb @@ -0,0 +1,3 @@ +module ActiveAdminAssociations + VERSION = "0.1.0" +end \ No newline at end of file diff --git a/lib/formtastic/inputs/token_input.rb b/lib/formtastic/inputs/token_input.rb new file mode 100644 index 0000000..e1287da --- /dev/null +++ b/lib/formtastic/inputs/token_input.rb @@ -0,0 +1,43 @@ +module Formtastic + module Inputs + class TokenInput + include Base + + def to_html + input_wrapping do + label_html << + builder.hidden_field(input_name, input_html_options) + end + end + + def input_html_options + super.merge({ + :required => nil, + :autofocus => nil, + :class => 'token-input', + 'data-model-name' => reflection.klass.model_name.singular + }).tap do |html_options| + if record.present? + html_options["data-pre"] = prepopulated_value.to_json + end + end + end + + def prepopulated_value + [{"value" => name_value, "id" => record.id}] + end + + def name_method + builder.collection_label_methods.find { |m| record.respond_to?(m) } + end + + def name_value + record.send(name_method) + end + + def record + @object.send(method) + end + end + end +end diff --git a/lib/formtastic/token_input_default_for_association.rb b/lib/formtastic/token_input_default_for_association.rb new file mode 100644 index 0000000..68b8f34 --- /dev/null +++ b/lib/formtastic/token_input_default_for_association.rb @@ -0,0 +1,19 @@ +module Formtastic + module TokenInputDefaultForAssociation + extend ActiveSupport::Concern + + included do + alias_method_chain :default_input_type, :token_default_for_association + end + + def default_input_type_with_token_default_for_association(method, options = {}) + if @object + reflection = reflection_for(method) + if reflection && reflection.klass.respond_to?(:autocomplete_attribute) && reflection.macro == :belongs_to + return :token + end + end + default_input_type_without_token_default_for_association(method, options) + end + end +end diff --git a/test/helper.rb b/test/helper.rb new file mode 100644 index 0000000..167b48a --- /dev/null +++ b/test/helper.rb @@ -0,0 +1,18 @@ +require 'rubygems' +require 'bundler' +begin + Bundler.setup(:default, :development) +rescue Bundler::BundlerError => e + $stderr.puts e.message + $stderr.puts "Run `bundle install` to install missing gems" + exit e.status_code +end +require 'test/unit' +require 'shoulda' + +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +$LOAD_PATH.unshift(File.dirname(__FILE__)) +require 'active_admin_form_config' + +class Test::Unit::TestCase +end diff --git a/test/test_active_form_config.rb b/test/test_active_form_config.rb new file mode 100644 index 0000000..87914a7 --- /dev/null +++ b/test/test_active_form_config.rb @@ -0,0 +1,7 @@ +require 'helper' + +class TestActiveAdminFormConfig < Test::Unit::TestCase + should "probably rename this file and start testing for real" do + flunk "hey buddy, you should probably rename this file and start testing for real" + end +end diff --git a/vendor/assets/javascripts/active_admin_associations.js b/vendor/assets/javascripts/active_admin_associations.js new file mode 100644 index 0000000..1ba5b03 --- /dev/null +++ b/vendor/assets/javascripts/active_admin_associations.js @@ -0,0 +1,14 @@ +//= require jquery.tokeninput + +$(document).ready(function(){ + $('input.token-input').tokenInput(function($input){ + var modelName = $input.data("model-name"); + return "/autocomplete/"+modelName; + }, { + minChars: 3, + propertyToSearch: "value", + theme: "facebook", + tokenLimit: 1, + preventDuplicates: true + }); +}); diff --git a/vendor/assets/javascripts/jquery.tokeninput.js b/vendor/assets/javascripts/jquery.tokeninput.js new file mode 100644 index 0000000..450e493 --- /dev/null +++ b/vendor/assets/javascripts/jquery.tokeninput.js @@ -0,0 +1,915 @@ +/* + * jQuery Plugin: Tokenizing Autocomplete Text Entry + * Version 1.6.0 + * + * Copyright (c) 2009 James Smith (http://loopj.com) + * Licensed jointly under the GPL and MIT licenses, + * choose which one suits your project best! + * + */ + +(function ($) { +// Default settings +var DEFAULT_SETTINGS = { + // Search settings + method: "GET", + queryParam: "q", + searchDelay: 300, + minChars: 1, + propertyToSearch: "name", + jsonContainer: null, + contentType: "json", + + // Prepopulation settings + prePopulate: null, + processPrePopulate: false, + + // Display settings + hintText: "Type in a search term", + noResultsText: "No results", + searchingText: "Searching...", + deleteText: "×", + animateDropdown: true, + theme: null, + zindex: 999, + resultsFormatter: function(item){ return "
  • " + item[this.propertyToSearch]+ "
  • " }, + tokenFormatter: function(item) { return "
  • " + item[this.propertyToSearch] + "

  • " }, + + // Tokenization settings + tokenLimit: null, + tokenDelimiter: ",", + preventDuplicates: false, + tokenValue: "id", + + // Callbacks + onResult: null, + onAdd: null, + onDelete: null, + onReady: null, + + // Other settings + idPrefix: "token-input-", + + // Keep track if the input is currently in disabled mode + disabled: false +}; + +// Default classes to use when theming +var DEFAULT_CLASSES = { + tokenList: "token-input-list", + token: "token-input-token", + tokenDelete: "token-input-delete-token", + selectedToken: "token-input-selected-token", + highlightedToken: "token-input-highlighted-token", + dropdown: "token-input-dropdown", + dropdownItem: "token-input-dropdown-item", + dropdownItem2: "token-input-dropdown-item2", + selectedDropdownItem: "token-input-selected-dropdown-item", + inputToken: "token-input-input-token", + focused: "token-input-focused", + disabled: "token-input-disabled" +}; + +// Input box position "enum" +var POSITION = { + BEFORE: 0, + AFTER: 1, + END: 2 +}; + +// Keys "enum" +var KEY = { + BACKSPACE: 8, + TAB: 9, + ENTER: 13, + ESCAPE: 27, + SPACE: 32, + PAGE_UP: 33, + PAGE_DOWN: 34, + END: 35, + HOME: 36, + LEFT: 37, + UP: 38, + RIGHT: 39, + DOWN: 40, + NUMPAD_ENTER: 108, + COMMA: 188 +}; + +// Additional public (exposed) methods +var methods = { + init: function(url_or_data_or_function, options) { + var settings = $.extend({}, DEFAULT_SETTINGS, options || {}); + + return this.each(function () { + $(this).data("tokenInputObject", new $.TokenList(this, url_or_data_or_function, settings)); + }); + }, + clear: function() { + this.data("tokenInputObject").clear(); + return this; + }, + add: function(item) { + this.data("tokenInputObject").add(item); + return this; + }, + remove: function(item) { + this.data("tokenInputObject").remove(item); + return this; + }, + get: function() { + return this.data("tokenInputObject").getTokens(); + }, + toggleDisabled: function(disable) { + this.data("tokenInputObject").toggleDisabled(disable); + return this; + } +} + +// Expose the .tokenInput function to jQuery as a plugin +$.fn.tokenInput = function (method) { + // Method calling and initialization logic + if(methods[method]) { + return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); + } else { + return methods.init.apply(this, arguments); + } +}; + +// TokenList class for each input +$.TokenList = function (input, url_or_data, settings) { + // + // Initialization + // + + // Configure the data source + if($.type(url_or_data) === "string" || $.type(url_or_data) === "function") { + // Set the url to query against + settings.url = url_or_data; + + // If the URL is a function, evaluate it here to do our initalization work + var url = computeURL(); + + // Make a smart guess about cross-domain if it wasn't explicitly specified + if(settings.crossDomain === undefined && typeof url === "string") { + if(url.indexOf("://") === -1) { + settings.crossDomain = false; + } else { + settings.crossDomain = (location.href.split(/\/+/g)[1] !== url.split(/\/+/g)[1]); + } + } + } else if(typeof(url_or_data) === "object") { + // Set the local data to search through + settings.local_data = url_or_data; + } + + // Build class names + if(settings.classes) { + // Use custom class names + settings.classes = $.extend({}, DEFAULT_CLASSES, settings.classes); + } else if(settings.theme) { + // Use theme-suffixed default class names + settings.classes = {}; + $.each(DEFAULT_CLASSES, function(key, value) { + settings.classes[key] = value + "-" + settings.theme; + }); + } else { + settings.classes = DEFAULT_CLASSES; + } + + + // Save the tokens + var saved_tokens = []; + + // Keep track of the number of tokens in the list + var token_count = 0; + + // Basic cache to save on db hits + var cache = new $.TokenList.Cache(); + + // Keep track of the timeout, old vals + var timeout; + var input_val; + + // Create a new text input an attach keyup events + var input_box = $("") + .css({ + outline: "none" + }) + .attr("id", settings.idPrefix + input.id) + .focus(function () { + if (settings.disabled) { + return false; + } else + if (settings.tokenLimit === null || settings.tokenLimit !== token_count) { + show_dropdown_hint(); + } + token_list.addClass(settings.classes.focused); + }) + .blur(function () { + hide_dropdown(); + $(this).val(""); + token_list.removeClass(settings.classes.focused); + }) + .bind("keyup keydown blur update", resize_input) + .keydown(function (event) { + var previous_token; + var next_token; + + switch(event.keyCode) { + case KEY.LEFT: + case KEY.RIGHT: + case KEY.UP: + case KEY.DOWN: + if(!$(this).val()) { + previous_token = input_token.prev(); + next_token = input_token.next(); + + if((previous_token.length && previous_token.get(0) === selected_token) || (next_token.length && next_token.get(0) === selected_token)) { + // Check if there is a previous/next token and it is selected + if(event.keyCode === KEY.LEFT || event.keyCode === KEY.UP) { + deselect_token($(selected_token), POSITION.BEFORE); + } else { + deselect_token($(selected_token), POSITION.AFTER); + } + } else if((event.keyCode === KEY.LEFT || event.keyCode === KEY.UP) && previous_token.length) { + // We are moving left, select the previous token if it exists + select_token($(previous_token.get(0))); + } else if((event.keyCode === KEY.RIGHT || event.keyCode === KEY.DOWN) && next_token.length) { + // We are moving right, select the next token if it exists + select_token($(next_token.get(0))); + } + } else { + var dropdown_item = null; + + if(event.keyCode === KEY.DOWN || event.keyCode === KEY.RIGHT) { + dropdown_item = $(selected_dropdown_item).next(); + } else { + dropdown_item = $(selected_dropdown_item).prev(); + } + + if(dropdown_item.length) { + select_dropdown_item(dropdown_item); + } + } + return false; + break; + + case KEY.BACKSPACE: + previous_token = input_token.prev(); + + if(!$(this).val().length) { + if(selected_token) { + delete_token($(selected_token)); + hidden_input.change(); + } else if(previous_token.length) { + select_token($(previous_token.get(0))); + } + + return false; + } else if($(this).val().length === 1) { + hide_dropdown(); + } else { + // set a timeout just long enough to let this function finish. + setTimeout(function(){do_search();}, 5); + } + break; + + case KEY.TAB: + case KEY.ENTER: + case KEY.NUMPAD_ENTER: + case KEY.COMMA: + if(selected_dropdown_item) { + add_token($(selected_dropdown_item).data("tokeninput")); + hidden_input.change(); + return false; + } + break; + + case KEY.ESCAPE: + hide_dropdown(); + return true; + + default: + if(String.fromCharCode(event.which)) { + // set a timeout just long enough to let this function finish. + setTimeout(function(){do_search();}, 5); + } + break; + } + }); + + // Keep a reference to the original input box + var hidden_input = $(input) + .hide() + .val("") + .focus(function () { + focus_with_timeout(input_box); + }) + .blur(function () { + input_box.blur(); + }); + + // Keep a reference to the selected token and dropdown item + var selected_token = null; + var selected_token_index = 0; + var selected_dropdown_item = null; + + // The list to store the token items in + var token_list = $("