Class: Admin::FieldsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/admin/fields_controller.rb

Overview

Copyright © 2008-2013 Michael Dvorkin and contributors.

Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or www.opensource.org/licenses/mit-license.php


Instance Method Summary collapse

Instance Method Details

#createObject

POST /fields POST /fields.xml AJAX




42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/admin/fields_controller.rb', line 42

def create
  as = params[:field][:as]
  @field = 
    if as =~ /pair/
      CustomFieldPair.create_pair(params).first
    elsif as.present?
      klass = Field.lookup_class(as).classify.constantize
      klass.create(params[:field])
    else
      Field.new(params[:field]).tap(&:valid?)
    end

  respond_with(@field)

end

#destroyObject

DELETE /fields/1 DELETE /fields/1.xml HTML and AJAX




75
76
77
78
79
80
# File 'app/controllers/admin/fields_controller.rb', line 75

def destroy
  @field = Field.find(params[:id])
  @field.destroy

  respond_with(@field)
end

#editObject

GET /fields/1/edit AJAX




34
35
36
37
# File 'app/controllers/admin/fields_controller.rb', line 34

def edit
  @field = Field.find(params[:id])
  respond_with(@field)    
end

#indexObject

GET /fields GET /fields.xml HTML




14
15
# File 'app/controllers/admin/fields_controller.rb', line 14

def index
end

#newObject

GET /fields/new GET /fields/new.xml AJAX




27
28
29
30
# File 'app/controllers/admin/fields_controller.rb', line 27

def new
  @field = Field.new
  respond_with(@field)
end

#showObject

GET /fields/1 GET /fields/1.xml HTML




20
21
22
# File 'app/controllers/admin/fields_controller.rb', line 20

def show
  respond_with(@field)
end

#sortObject

POST /fields/sort




84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/admin/fields_controller.rb', line 84

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

  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

#subformObject

GET /fields/subform




97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/admin/fields_controller.rb', line 97

def subform
  field = params[:field]
  as = field[:as]

  @field = if (id = field[:id]).present?
      Field.find(id).tap{|f| f.as = as}
    else
      field_group_id = field[:field_group_id]
      klass = Field.lookup_class(as).classify.constantize
      klass.new(:field_group_id => field_group_id, :as => as)
    end

  respond_with(@field) do |format|
    format.html { render :partial => 'admin/fields/subform' }
  end  
end

#updateObject

PUT /fields/1 PUT /fields/1.xml AJAX




61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/admin/fields_controller.rb', line 61

def update
  if (params[:field][:as] =~ /pair/)
    @field = CustomFieldPair.update_pair(params).first
  else
    @field = Field.find(params[:id])
    @field.update_attributes(params[:field])
  end

  respond_with(@field)
end