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




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

def create
  as = field_params[:as]
  @field =
    if as.match?(/pair/)
      CustomFieldPair.create_pair(params).first
    elsif as.present?
      klass = find_class(Field.lookup_class(as))
      klass.create(field_params)
    else
      Field.new(field_params).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




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

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

#indexObject

GET /fields GET /fields.xml HTML




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

def index
end

#newObject

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




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

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

#showObject

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




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

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.where(id: id).update_all(position: index + 1, field_group_id: field_group_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 = field_params
  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 = find_class(Field.lookup_class(as))
             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 field_params[:as].match?(/pair/)
    @field = CustomFieldPair.update_pair(params).first
  else
    @field = Field.find(params[:id])
    @field.update_attributes(field_params)
  end

  respond_with(@field)
end