Class: DynamicFieldsets::FieldsController

Inherits:
ApplicationController
  • Object
show all
Includes:
FieldsHelper
Defined in:
app/controllers/dynamic_fieldsets/fields_controller.rb

Instance Method Summary collapse

Methods included from NestedModelHelper

#df_link_to_add_fields, #df_link_to_remove_fields

Instance Method Details

#createObject

POST /dynamic_fieldsets/fields



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/dynamic_fieldsets/fields_controller.rb', line 55

def create
  parent_id = params[:parent]
  @field = params[:dynamic_fieldsets_field][:type].constantize.new(params[:dynamic_fieldsets_field])

  respond_to do |format|
    type = params[:dynamic_fieldsets_field][:type] 
    
    if  type == "" 
      format.html { redirect_to(new_dynamic_fieldsets_field_path, :notice => 'Need to select a type') }
    
    elsif (type == "DynamicFieldsets::CheckboxField" || type == "DynamicFieldsets::RadioField" || type == "DynamicFieldsets::MultipleSelectField" || type == "DynamicFieldsets::SelectField") && !params[:dynamic_fieldsets_field].has_key?(:field_options_attributes)
      format.html { redirect_to(new_dynamic_fieldsets_field_path, :notice => 'Need to add at least one option with field type '+type.split('::').last) }
    
    else
      if @field.save
        if !parent_id.empty?
          parent = DynamicFieldsets::Fieldset.find(parent_id)
          DynamicFieldsets::FieldsetChild.create( :fieldset => parent, :child => @field )
          #relation = @fieldset.fieldset_children.build( :fieldset => parent )
          #relation.child = @field
          #relation.save
        end
        format.html { redirect_to(dynamic_fieldsets_field_path(@field), :notice => 'Successfully created a new field.') }
      else
        format.html { render :action => "new" }
      end
    end
  end
end

#destroyObject

DELETE /dynamic_fieldsets/fields/1 DELETE /dynamic_fieldsets/fields/1.xml



113
114
115
116
117
118
119
120
# File 'app/controllers/dynamic_fieldsets/fields_controller.rb', line 113

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

  respond_to do |format|
    format.html { redirect_to(dynamic_fieldsets_fields_url) }
  end
end

#editObject

GET /dynamic_fieldsets/fields/1/edit



33
34
35
# File 'app/controllers/dynamic_fieldsets/fields_controller.rb', line 33

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

#enableObject

Custom controller action to set the enabled field on the field object Expects a url with an id field and a form with the value [:dynamic_fieldsets_field] On success or failure, redirects to the index page with a notice

This should never fail the save unless the field was initially saved without validations



42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/dynamic_fieldsets/fields_controller.rb', line 42

def enable
  @field = DynamicFieldsets::Field.find(params[:id])
  @field.enabled = params[:dynamic_fieldsets_field][:enabled]
  respond_to do |format|
    if @field.save
      format.html { redirect_to(dynamic_fieldsets_fields_path, :notice => 'Successfully updated a new field.') }
    else
      format.html { redirect_to(dynamic_fieldsets_fields_path, :notice => 'Did not update the field successfully.') }
    end
  end
end

#indexObject

GET /dynamic_fieldsets/fields



6
7
8
9
10
11
12
# File 'app/controllers/dynamic_fieldsets/fields_controller.rb', line 6

def index
  @fields = DynamicFieldsets::Field.all

  respond_to do |format|
    format.html # index.html.erb
  end
end

#newObject

GET /dynamic_fieldsets/fields/new



24
25
26
27
28
29
30
# File 'app/controllers/dynamic_fieldsets/fields_controller.rb', line 24

def new
  @field = DynamicFieldsets::Field.new

  respond_to do |format|
    format.html # new.html.erb
  end
end

#showObject

GET /dynamic_fieldsets/fields/1



15
16
17
18
19
20
21
# File 'app/controllers/dynamic_fieldsets/fields_controller.rb', line 15

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

  respond_to do |format|
    format.html # show.html.erb
  end
end

#updateObject

PUT /dynamic_fieldsets/fields/1



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/dynamic_fieldsets/fields_controller.rb', line 86

def update
  # note that on update, we need to get a Field object so that the type can be changed
  @field = DynamicFieldsets::Field.find(params[:id])

  respond_to do |format|
    # this is sort of a bad hack to make field type changes work
    # it is setup to allow validations to stop the type from changing using validations
    # this could be improved by figuring out how to set the type using update_attributes (JH 3-2-2012)
    if @field.type != params[:dynamic_fieldsets_field][:type]
      @field.type = params[:dynamic_fieldsets_field][:type]
      if @field.save
        @field = DynamicFieldsets::Field.find(params[:id])
      else
        format.html { render :action => "edit", :notice => 'Could not change the field type.' }
      end
    end
    
    if @field.update_attributes(params[:dynamic_fieldsets_field])
      format.html { redirect_to(dynamic_fieldsets_field_path(@field), :notice => 'Successfully updated a field.') }
    else
      format.html { render :action => "edit" }
    end
  end
end