Skip to content

Commit

Permalink
Adding field group admin features
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Tillman committed Dec 6, 2011
1 parent fa2d852 commit ed8f456
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 47 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/field_groups.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/admin/field_groups.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Admin::FieldGroups controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
65 changes: 65 additions & 0 deletions app/controllers/admin/field_groups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Fat Free CRM
# Copyright (C) 2008-2011 by Michael Dvorkin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------------------------

class Admin::FieldGroupsController < Admin::ApplicationController
before_filter :require_user

# GET /field_groups/new
# GET /field_groups/new.xml AJAX
#----------------------------------------------------------------------------
def new
@field_group = FieldGroup.new(:klass_name => params[:klass_name])

respond_to do |format|
format.js # new.js.rjs
format.xml { render :xml => @field_group }
end

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

# POST /field_groups
# POST /field_groups.xml AJAX
#----------------------------------------------------------------------------
def create
@field_group = FieldGroup.new(params[:field_group])

respond_to do |format|
if @field_group.save
format.js # create.js.rjs
format.xml { render :xml => @field_group, :status => :created, :location => @field_group }
else
format.js # create.js.rjs
format.xml { render :xml => @field_group.errors, :status => :unprocessable_entity }
end
end
end

# POST /field_groups/sort
#----------------------------------------------------------------------------
def sort
asset = params[:asset]
field_group_ids = params["#{asset}_field_groups"]

field_group_ids.each_with_index do |id, index|
FieldGroup.update_all({:position => index+1}, {:id => id})
end

render :nothing => true
end
end
37 changes: 14 additions & 23 deletions app/controllers/admin/fields_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Fat Free CRM
# Copyright (C) 2008-2009 by Michael Dvorkin
# Copyright (C) 2008-2011 by Michael Dvorkin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand All @@ -20,28 +20,6 @@ class Admin::FieldsController < Admin::ApplicationController
before_filter :set_current_tab, :only => [ :index, :show ]
before_filter :auto_complete, :only => :auto_complete

def sort
field_group_id = params[:field_group_id].to_i
field_ids = params["field_group_#{field_group_id}_fields"] || []

field_ids.each_with_index do |id, index|
Field.update_all({:position => index+1, :field_group_id => field_group_id}, {:id => id})
end

render :nothing => true
end

def group_sort
asset = params[:asset]
field_group_ids = params["#{asset}_field_groups"]

field_group_ids.each_with_index do |id, index|
FieldGroup.update_all({:position => index+1}, {:id => id})
end

render :nothing => true
end

# GET /fields
# GET /fields.xml HTML
#----------------------------------------------------------------------------
Expand Down Expand Up @@ -144,6 +122,19 @@ def destroy
respond_to_not_found(:html, :js, :xml)
end

# POST /fields/sort
#----------------------------------------------------------------------------
def sort
field_group_id = params[:field_group_id].to_i
field_ids = params["field_group_#{field_group_id}_fields"] || []

field_ids.each_with_index do |id, index|
Field.update_all({:position => index+1, :field_group_id => field_group_id}, {:id => id})
end

render :nothing => true
end

# POST /fields/auto_complete/query AJAX
#----------------------------------------------------------------------------
# Handled by before_filter :auto_complete, :only => :auto_complete
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/admin/field_groups_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Admin::FieldGroupsHelper
end
13 changes: 13 additions & 0 deletions app/views/admin/field_groups/_field_group.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.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))

.title
= t(field_group.name, :default => field_group.label)

- group_list_id = "field_group_#{field_group.id}_fields"
.list{ :id => group_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)
9 changes: 9 additions & 0 deletions app/views/admin/field_groups/_new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

= form_for([:admin, @field_group], :html => one_submit_only(:field_group), :remote => true) do |f|
= link_to_close new_admin_field_group_path
= f.error_messages
= render :partial => "admin/field_groups/top_section", :locals => { :f => f }
.buttonbar
= f.submit "Create field group"
or
= link_to_cancel new_admin_field_group_path
12 changes: 12 additions & 0 deletions app/views/admin/field_groups/_top_section.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.section
= f.hidden_field :klass_name

%table
%tr
%td
.label.top.req Label:
= f.text_field :label
%td= spacer
%td
.label.top.req Tag:
= f.select :tag_id, ActsAsTaggableOn::Tag.all.map { |tag| [tag.name, tag.id] }
14 changes: 14 additions & 0 deletions app/views/admin/field_groups/create.js.rjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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[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
end

9 changes: 9 additions & 0 deletions app/views/admin/field_groups/new.js.rjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
page.call "crm.flick", :empty, :toggle
page.call "crm.flip_form", :create_field_group

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"
end
5 changes: 3 additions & 2 deletions app/views/admin/fields/create.js.rjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ 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("")}'
page.insert_html :top, "#{@field.field_group_id}_fields", :partial => "field", :collection => [ @field ]
group_list_id = "field_group_#{@field.field_group_id}_fields"
page.insert_html :bottom, group_list_id, :partial => "field", :collection => [ @field ]
page[dom_id(@field)].visual_effect :highlight, :duration => 1.5
page.call "crm.flick", :empty, :remove
page.insert_html :bottom, "#{@field.field_group_id}_fields", sortable_element("#{@field.field_group_id}_fields", :url => sort_admin_fields_path(:field_group_id => @field.field_group_id))
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
Expand Down
32 changes: 11 additions & 21 deletions app/views/admin/fields/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,30 @@
.title
%span#create_field_title Fields

.remote#options{ hidden }

.inline_tabs
%ul
- Field::KLASSES.each do |klass|
%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) }

- group_list_ids = []
- asset_list_id = "#{asset}_field_groups"
.list{ :id => asset_list_id }

- klass.field_groups.each do |field_group|

.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))
.title_tools
= link_to_inline(:create_field_group, new_admin_field_group_path(:klass_name => klass.name), :text => t(:create_field_group))

.title
= t(field_group.name, :default => field_group.label)
.title
= t(asset)

- group_list_id = "field_group_#{field_group.id}_fields"
- group_list_ids << group_list_id
.list{ :id => group_list_id }
- if (fields = field_group.fields).present?
= render :partial => "field", :collection => fields
- asset_list_id = "#{asset}_field_groups"
- group_list_ids = klass.field_groups.map {|field_group| "field_group_#{field_group.id}_fields"}

- klass.field_groups.each do |field_group|
= sortable_element("field_group_#{field_group.id}_fields", :url => sort_admin_fields_path(:field_group_id => field_group.id), :containment => group_list_ids, :dropOnEmpty => true)
.list{ :id => asset_list_id }
= render :partial => "fields_groups/field_group", :collection => klass.field_groups, :locals => {:group_list_ids => group_list_ids}

= sortable_element(asset_list_id, :url => group_sort_admin_fields_path(:asset => asset), :tag => :div)
= sortable_element(asset_list_id, :url => sort_admin_field_groups_path(:asset => asset), :tag => :div)
1 change: 1 addition & 0 deletions config/locales/en-US_fat_free_crm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,4 @@ en-US:
#----------------------------------------------------------------------------
custom_fields: Custom Fields
create_custom_field: Create Custom Field
create_field_group: Create Field Group
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,19 @@
end
end

resources :field_groups, :except => :index do
collection do
post :sort
end
end

resources :fields do
collection do
post :auto_complete
get :options
post :redraw
get :search
post :sort
post :group_sort
end
end
resources :fields, :as => :custom_fields
Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/admin/field_groups_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe Admin::FieldGroupsController do

end
15 changes: 15 additions & 0 deletions spec/helpers/admin/field_groups_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the Admin::FieldGroupsHelper. For example:
#
# describe Admin::FieldGroupsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe Admin::FieldGroupsHelper do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit ed8f456

Please sign in to comment.