Skip to content

Commit

Permalink
Management features for field groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Tillman committed Dec 6, 2011
1 parent ed8f456 commit 27f4e0b
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 58 deletions.
34 changes: 34 additions & 0 deletions app/controllers/admin/field_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ def new
respond_to_not_found(:html, :xml)
end

# GET /field_groups/1/edit AJAX
#----------------------------------------------------------------------------
def edit
@field_group = FieldGroup.find(params[:id])

if params[:previous].to_s =~ /(\d+)\z/
@previous = FieldGroup.find($1)
end

rescue ActiveRecord::RecordNotFound
@previous ||= $1.to_i
respond_to_not_found(:js)
end

# POST /field_groups
# POST /field_groups.xml AJAX
#----------------------------------------------------------------------------
Expand All @@ -50,6 +64,26 @@ def create
end
end

# PUT /field_groups/1
# PUT /field_groups/1.xml AJAX
#----------------------------------------------------------------------------
def update
@field_group = FieldGroup.find(params[:id])

respond_to do |format|
if @field_group.update_attributes(params[:field_group])
format.js
format.xml { head :ok }
else
format.js
format.xml { render :xml => @field_group.errors, :status => :unprocessable_entity }
end
end

rescue ActiveRecord::RecordNotFound
respond_to_not_found(:js, :xml)
end

# POST /field_groups/sort
#----------------------------------------------------------------------------
def sort
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/fields_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def edit

rescue ActiveRecord::RecordNotFound
@previous ||= $1.to_i
respond_to_not_found(:js) unless @custom_field
respond_to_not_found(:js)
end

# POST /fields
Expand Down Expand Up @@ -99,7 +99,7 @@ def update
format.xml { head :ok }
else
format.js
format.xml { render :xml => @custom_field.errors, :status => :unprocessable_entity }
format.xml { render :xml => @field.errors, :status => :unprocessable_entity }
end
end

Expand Down
5 changes: 3 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,17 @@ def link_to_discard(model)

#----------------------------------------------------------------------------
def link_to_cancel(url, params = {})
url = params[:url] if params[:url]
link_to(t(:cancel),
(params[:url] || url) + '?cancel=true',
url + "#{url.include?('?') ? '&' : '?'}cancel=true",
:remote => true
)
end

#----------------------------------------------------------------------------
def link_to_close(url)
content_tag(:div,
link_to("x", url + '?cancel=true', :remote => true),
link_to("x", url + "#{url.include?('?') ? '&' : '?'}cancel=true", :remote => true),
:class => "close",
:title => t(:close_form),
:onmouseover => "this.style.background='lightsalmon'",
Expand Down
2 changes: 2 additions & 0 deletions app/models/fields/field_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class FieldGroup < ActiveRecord::Base
has_many :fields
belongs_to :tag

validates_presence_of :label

before_save do
self.name = label.downcase.gsub(/[^a-z0-9]+/, '_') if name.blank? and label.present?
end
Expand Down
8 changes: 8 additions & 0 deletions app/views/admin/field_groups/_edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
= form_for([:admin, @field_group], :html => one_submit_only(:field_group), :remote => true) do |f|
= link_to_close edit_admin_field_group_path(@field_group)
= f.error_messages
= render :partial => "top_section", :locals => { :f => f, :edit => true }
.buttonbar
= f.submit t(:save_field_group), :id => :field_group_submit
or
= link_to_cancel edit_admin_field_group_path(@field_group)
20 changes: 14 additions & 6 deletions app/views/admin/field_groups/_field_group.html.haml
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
.field_group[field_group]
.title_tools
= link_to_inline(:create_field, new_admin_field_path(:field_group_id => field_group.id), :text => t(:create_custom_field))
- edit_form_id = dom_id(field_group, :edit)
- create_form_id = dom_id(field_group, :create_field)

.title
.subtitle_tools
= link_to_inline(edit_form_id, edit_admin_field_group_path(field_group), :text => t(:edit_field_group))
|
= link_to_inline(create_form_id, new_admin_field_path(:field_group_id => field_group.id), :text => t(:create_field))

.subtitle{ :id => "#{dom_id(field_group)}_title" }
= t(field_group.name, :default => field_group.label)

- group_list_id = "field_group_#{field_group.id}_fields"
.list{ :id => group_list_id }
.remote{ hidden.merge(:id => edit_form_id) }
.remote{ hidden.merge(:id => create_form_id) }

- list_id = dom_id(field_group, :fields)
.list{ :id => list_id }
- if (fields = field_group.fields).present?
= render :partial => "admin/fields/field", :collection => fields

= sortable_element(group_list_id, :url => sort_admin_fields_path(:field_group_id => field_group.id), :containment => group_list_ids, :dropOnEmpty => true)
= sortable_element(list_id, :url => sort_admin_fields_path(:field_group_id => field_group.id), :containment => group_list_ids, :dropOnEmpty => true)
9 changes: 5 additions & 4 deletions app/views/admin/field_groups/_new.html.haml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
- path = new_admin_field_group_path(:klass_name => @field_group.klass_name)

= form_for([:admin, @field_group], :html => one_submit_only(:field_group), :remote => true) do |f|
= link_to_close new_admin_field_group_path
= link_to_close path
= f.error_messages
= render :partial => "admin/field_groups/top_section", :locals => { :f => f }
= render :partial => "top_section", :locals => { :f => f }
.buttonbar
= f.submit "Create field group"
= f.submit t(:create_field_group), :id => :field_group_submit
or
= link_to_cancel new_admin_field_group_path
= link_to_cancel path
4 changes: 2 additions & 2 deletions app/views/admin/field_groups/_top_section.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
= f.text_field :label
%td= spacer
%td
.label.top.req Tag:
= f.select :tag_id, ActsAsTaggableOn::Tag.all.map { |tag| [tag.name, tag.id] }
.label.top Tag:
= f.select :tag_id, ActsAsTaggableOn::Tag.all.map { |tag| [tag.name, tag.id] }, :include_blank => true
19 changes: 11 additions & 8 deletions app/views/admin/field_groups/create.js.rjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
asset = @field_group.klass_name.downcase
id = "#{asset}_create_field_group"
container_id = "#{asset}_field_groups"

if @field_group.valid?
page[:create_field_group_arrow].replace_html "&#9658;"
page.visual_effect :toggle_blind, :create_field_group, :duration => 0.3, :afterFinish => 'function(effect) {effect.element.update("")}'
asset_list_id = "#{@field_group.klass_name.downcase}_field_groups"
page.insert_html :bottom, asset_list_id, :partial => "field_group", :collection => [ @field_group ]
page["#{id}_arrow"].replace_html "&#9658;"
page.visual_effect :toggle_blind, id, :duration => 0.3, :afterFinish => 'function(effect) {effect.element.update("")}'
group_list_ids = @field_group.klass.field_groups.map {|field_group| dom_id(field_group, :fields)}
page.insert_html :top, container_id, :partial => "field_group", :collection => [ @field_group ], :locals => {:group_list_ids => group_list_ids}
page.insert_html :bottom, container_id, sortable_element(container_id, :url => sort_admin_field_groups_path(:asset => asset), :tag => :div)
page[dom_id(@field_group)].visual_effect :highlight, :duration => 1.5
page.call "crm.flick", :empty, :remove
page.insert_html :bottom, asset_list_id, sortable_element(asset_list_id, :url => sort_admin_field_groups_path(:asset => asset))
else
page[:create_field_group].replace_html :partial => "new"
page[:create_field_group].visual_effect :shake, :duration => 0.25, :distance => 6
page[:field_label].focus
page[id].replace_html :partial => "new"
page[id].visual_effect :shake, :duration => 0.25, :distance => 6
end

14 changes: 14 additions & 0 deletions app/views/admin/field_groups/edit.js.rjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
id = dom_id(@field_group)
form_id = dom_id(@field_group, :edit)

if params[:cancel] == "true" # <----------------- Hide [Edit field_group]

page.call "crm.flip_form", form_id
page.call "crm.set_title", id, @field_group.label

else # <----------------------------------------- Show [Edit field_group] form.

page.call "crm.show_form", form_id
page[form_id].replace_html :partial => "edit"

end
10 changes: 5 additions & 5 deletions app/views/admin/field_groups/new.js.rjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
asset = @field_group.klass_name.downcase
id = "#{asset}_create_field_group"

page.call "crm.flick", :empty, :toggle
page.call "crm.flip_form", :create_field_group
page.call "crm.flip_form", id

if params[:cancel] != "true"
page[:create_field_group].replace_html :partial => "new"
page.call "crm.set_title", :create_field_group
else
page.call "crm.set_title", :create_field_group, "Custom field groups"
page[id].replace_html :partial => "new"
end
9 changes: 9 additions & 0 deletions app/views/admin/field_groups/update.js.rjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
id = dom_id(@field_group, :edit)

if @field_group.errors.empty?
page.call "crm.flip_form", id
page.call "crm.set_title", dom_id(@field_group), @field_group.label
else
page[id].replace_html :partial => "edit"
page[id].visual_effect :shake, :duration => 0.25, :distance => 6
end
4 changes: 2 additions & 2 deletions app/views/admin/fields/_edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
= form_for([:admin, @field], :as => :field, :html => one_submit_only(:field), :remote => true) do |f|
= link_to_close edit_admin_field_path(@field)
= f.error_messages
= render :partial => "admin/fields/top_section", :locals => { :f => f, :edit => true }
= render :partial => "top_section", :locals => { :f => f, :edit => true }
.buttonbar
= f.submit "Save field"
= f.submit t(:save_field), :id => :field_submit
or
= link_to_cancel edit_admin_field_path(@field)
9 changes: 5 additions & 4 deletions app/views/admin/fields/_new.html.haml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
- path = new_admin_field_path(:field_group_id => @field.field_group_id)

= form_for([:admin, @field], :as => :field, :html => one_submit_only(:field), :remote => true) do |f|
= link_to_close new_admin_field_path
= link_to_close path
= f.error_messages
= render :partial => "admin/fields/top_section", :locals => { :f => f }
= render :partial => "top_section", :locals => { :f => f }
.buttonbar
= f.submit "Create field"
= f.submit t(:create_field), :id => :field_submit
or
= link_to_cancel new_admin_field_path
= link_to_cancel path
17 changes: 9 additions & 8 deletions app/views/admin/fields/create.js.rjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
id = dom_id(@field.field_group, :create_field)
container_id = dom_id(@field.field_group, :fields)

if @field.valid?
page[:create_field_arrow].replace_html "&#9658;"
page[:create_field_title].replace_html "fields"
page.visual_effect :toggle_blind, :create_field, :duration => 0.3, :afterFinish => 'function(effect) {effect.element.update("")}'
group_list_id = "field_group_#{@field.field_group_id}_fields"
page.insert_html :bottom, group_list_id, :partial => "field", :collection => [ @field ]
page["#{id}_arrow"].replace_html "&#9658;"
page.visual_effect :toggle_blind, id, :duration => 0.3, :afterFinish => 'function(effect) {effect.element.update("")}'
page.insert_html :top, container_id, :partial => "field", :collection => [ @field ]
page.insert_html :bottom, container_id, sortable_element(container_id, :url => sort_admin_fields_path(:field_group_id => @field.field_group_id))
page[dom_id(@field)].visual_effect :highlight, :duration => 1.5
page.call "crm.flick", :empty, :remove
page.insert_html :bottom, group_list_id, sortable_element(group_list_id, :url => sort_admin_fields_path(:field_group_id => @field.field_group_id))
else
page[:create_field].replace_html :partial => "new"
page[:create_field].visual_effect :shake, :duration => 0.25, :distance => 6
page[id].replace_html :partial => "new"
page[id].visual_effect :shake, :duration => 0.25, :distance => 6
page[:field_label].focus
end

14 changes: 7 additions & 7 deletions app/views/admin/fields/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
%li{ "data-tab-class" => klass.name.downcase, :class => ("selected" if klass == Contact) }
= link_to t(klass.name.downcase), "#"

.remote#create_field_group{ hidden }

.remote#create_field{ hidden }

- Field::KLASSES.each do |klass|
- asset = klass.name.downcase
.fields{ :id => "#{asset}_section", :style => ("display: none;" if klass != Contact) }

- create_form_id = "#{asset}_create_field_group"

.title_tools
= link_to_inline(:create_field_group, new_admin_field_group_path(:klass_name => klass.name), :text => t(:create_field_group))
= link_to_inline(create_form_id, new_admin_field_group_path(:klass_name => klass.name), :text => t(:create_field_group))

.title
= t(asset)

.remote{ hidden.merge(:id => create_form_id) }

- asset_list_id = "#{asset}_field_groups"
- group_list_ids = klass.field_groups.map {|field_group| "field_group_#{field_group.id}_fields"}
- group_list_ids = klass.field_groups.map {|field_group| dom_id(field_group, :fields)}

.list{ :id => asset_list_id }
= render :partial => "fields_groups/field_group", :collection => klass.field_groups, :locals => {:group_list_ids => group_list_ids}
= render :partial => "admin/field_groups/field_group", :collection => klass.field_groups, :locals => {:group_list_ids => group_list_ids}

= sortable_element(asset_list_id, :url => sort_admin_field_groups_path(:asset => asset), :tag => :div)
9 changes: 4 additions & 5 deletions app/views/admin/fields/new.js.rjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
id = dom_id(@field.field_group, :create_field)

page.call "crm.flick", :empty, :toggle
page.call "crm.flip_form", :create_field
page.call "crm.flip_form", id

if params[:cancel] != "true"
page[:create_field].replace_html :partial => "new"
page.call "crm.set_title", :create_field
else
page.call "crm.set_title", :create_field, "Custom fields"
page[id].replace_html :partial => "new"
end
3 changes: 2 additions & 1 deletion app/views/admin/fields/update.js.rjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
id = called_from_landing_page? ? "edit_field" : dom_id(@field)
container_id = "#{@field.klass_name.downcase}_fields"

if @field.errors.empty?
if called_from_landing_page?
Expand All @@ -7,7 +8,7 @@ if @field.errors.empty?
else
page[id].replace :partial => "field", :collection => [ @field ]
page[id].visual_effect :highlight, :duration => 1.0
sortable_element("#{@field.klass_name.downcase}_fields", :url => sort_admin_fields_path(:klass_name => @field.klass.name))
sortable_element(container_id, :url => sort_admin_fields_path(:klass_name => @field.klass.name))
end
else
page[id].replace_html :partial => "edit"
Expand Down
7 changes: 5 additions & 2 deletions config/locales/en-US_fat_free_crm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -684,5 +684,8 @@ en-US:
# Views -> Admin -> Custom Fields
#----------------------------------------------------------------------------
custom_fields: Custom Fields
create_custom_field: Create Custom Field
create_field_group: Create Field Group
create_field: Create field
save_field: Save field
create_field_group: Create field group
edit_field_group: Edit field group
save_field_group: Save field group

0 comments on commit 27f4e0b

Please sign in to comment.