Skip to content

Commit

Permalink
Show and hide field groups based on selected tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Tillman committed Nov 22, 2011
1 parent afbce41 commit a45a10b
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 18 deletions.
13 changes: 13 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -92,6 +92,19 @@ def tagged
redirect_to :action => "index"
end

def field_group
if params[:tags]
klass = controller_name.classify.constantize
tags = params[:tags].split(",")
@tags = tags.map do |t|
ActsAsTaggableOn::Tag.where(:name => t.strip).where('taggable_type IS NULL OR taggable_type = ?', klass.name).first
end.compact.uniq
@asset = klass.find_by_id(params[:asset_id]) || klass.new
render :template => 'shared/tags.js'
else
render :text => ''
end
end
private
#----------------------------------------------------------------------------
def set_context
Expand Down
1 change: 1 addition & 0 deletions app/models/fields/field_group.rb
@@ -1,5 +1,6 @@
class FieldGroup < ActiveRecord::Base
has_many :fields
belongs_to :tag

def key
"field_group_#{id}"
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.haml
Expand Up @@ -12,7 +12,7 @@
= javascript_include_tag :defaults, 'modalbox.js', 'facebooklist.js', 'facebooklist.simulate.js', :cache => "cache/all"
- unless tabless_layout?
= javascript_include_tag "crm_classes.js", "calendar_date_select/calendar_date_select.js", "calendar_date_select/format_#{t(:calendar_date_select_format, :default => 'american')}.js", :cache => "cache/classes"
= javascript_include_tag "crm_classes.js", 'fields.js', "calendar_date_select/calendar_date_select.js", "calendar_date_select/format_#{t(:calendar_date_select_format, :default => 'american')}.js", :cache => "cache/classes"

= hook(:javascript_includes, self)

Expand Down
47 changes: 31 additions & 16 deletions app/views/shared/_custom_fields.html.haml
@@ -1,23 +1,38 @@
- asset = controller_name.singularize
- f.object.tags = unsaved_param_tags(asset) if params[asset] and params[asset][:tag_list]
- field_groups = {}

- f.object.class.custom_fields.group_by(&:field_group_id).each do |field_group_id, fields|

- field_group = FieldGroup.find(field_group_id)
- next unless field_group.tag_id.nil? or f.object.tags.map(&:id).include?(field_group.tag_id)
- tag = f.object.tags.detect { |tag| tag.id == field_group.tag_id }

- next unless tag.present? or field_group.tag_id.nil?

- collapsed = session[field_group.key].nil?
= subtitle field_group.key, collapsed, field_group.name
.section
%small{ hidden_if(!collapsed).merge(:id => "#{field_group.key}_intro") }
%div[field_group]{ hidden_if(collapsed) }
%table
- fields.sort_by(&:position).in_groups_of(2) do |group|
%tr
%td
- if (field = group[0])
= f.input field.name, field.input_options
%td
= spacer
%td
- if (field = group[1])
= f.input field.name, field.input_options
- container_id = "#{field_group.key}_container"
- field_groups[tag.name.downcase] = container_id
%div{ :id => container_id }
= subtitle field_group.key, collapsed, field_group.name
.section
%small{ hidden_if(!collapsed).merge(:id => "#{field_group.key}_intro") }
%div[field_group]{ hidden_if(collapsed) }
%table
- fields.sort_by(&:position).in_groups_of(2) do |group|
%tr
%td
- if (field = group[0])
= f.input field.name, field.input_options
%td
= spacer
%td
- if (field = group[1])
= f.input field.name, field.input_options

:javascript
// if fbtaglist is initialized..
if (fbtaglist) {
crm.set_tag_list_event('#{controller_name}', '#{asset}', '#{params[:id]}');
// Reset 'loadedSupertagForms' hash.
loadedFieldGroups = $H(#{field_groups.to_json});
}
2 changes: 1 addition & 1 deletion app/views/shared/_tags.html.haml
Expand Up @@ -7,7 +7,7 @@
#facebook-list
= f.text_field :tag_list, :id => "tag_list", :style => "width:500px", :autocomplete => "off"
#facebook-auto
.default Type the name of a tag you'd like to use. Use commas to separate multiple tags.
.default Type the name of a tag you'd like to use. Use commas to separate multiple tags.
%ul.feed
- # Get tags from the object.
- f.object.tags.map{|t| t.name }.each do |tag|
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -141,6 +141,7 @@
end

get '/:controller/tagged/:id' => '#tagged'
get '/:controller/field_group' => '#field_group'
end
end

54 changes: 54 additions & 0 deletions public/javascripts/fields.js
@@ -0,0 +1,54 @@
//----------------------------------------------------------------------------
// Adds the 'on_change' hook for the FacebookList, to AJAX load custom field groups.
crm.set_tag_list_event = function(controller, asset, asset_id) {
var extra_field_group_options = $H({
onAdd: function(tag, el){
// Check that the tag is not being added more than twice (case INSENSITIVE)
var alreadyAdded = (fbtaglist.bits.values().findAll(function(s){return s.toLowerCase() == tag.toLowerCase() }).length > 1);
if(alreadyAdded){
// turn off the onDispose hook for this call to .dispose()
var onDisposeHook = fbtaglist.options.get('onDispose');
fbtaglist.options.set('onDispose', function(el){});
fbtaglist.dispose(el);
fbtaglist.options.set('onDispose', onDisposeHook);
} else {
// load the field group if not already loaded.
crm.load_field_group(controller, tag, asset_id);
};
},
onDispose: function(tag){
// remove the field group if it was loaded.
tag = tag.toLowerCase();
var form_id = loadedFieldGroups.get(tag);
if(form_id){
$(form_id).remove();
loadedFieldGroups.unset(tag);
};
}
});
fbtaglist.options.update(extra_field_group_options);
};

//----------------------------------------------------------------------------
// AJAX loads the form fields for each field group
crm.load_field_group = function(controller, tags, asset_id) {
new Ajax.Request('/' + controller + '/field_groups', {
asynchronous : true,
evalScripts:true,
method:'get',
parameters: { tags : tags,
asset_id : asset_id,
collapsed : "no" }
});
};

//----------------------------------------------------------------------------
// Fires an 'onclick' event on all '.close' buttons in the DOM.
// (closes any current edit forms)
crm.close_all_forms = function() {
$$('.close').each(function(el){el.onclick();});
};

// Initialize the hash to store which field groups have been loaded.
// {'tag' => 'div element id'}
var loadedFieldGroups = new Hash();

0 comments on commit a45a10b

Please sign in to comment.