Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjlandau committed May 4, 2012
0 parents commit 5c3c98f
Show file tree
Hide file tree
Showing 29 changed files with 1,816 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .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
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gemspec
20 changes: 20 additions & 0 deletions 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.
165 changes: 165 additions & 0 deletions 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 = "<span id=\"speaker-#{speaker.id}\">#{speaker.name} <em>("
label << "#{speaker.position}, " unless speaker.position.blank?
label << "#{speaker.talk_count} talk#{'s' unless speaker.talk_count == 1})</em></span>"
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.
10 changes: 10 additions & 0 deletions 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
26 changes: 26 additions & 0 deletions 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
33 changes: 33 additions & 0 deletions 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
75 changes: 75 additions & 0 deletions 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
@@ -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 -%>

0 comments on commit 5c3c98f

Please sign in to comment.