Class: ConstructorPages::FieldsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/constructor_pages/fields_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

movable

Instance Method Details

#createObject


13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/constructor_pages/fields_controller.rb', line 13

def create
  @field = Field.new field_params
  @template = @field.template

  if @field.save
    redirect_to edit_template_path(@template), notice: t(:field_success_added, name: @field.name)
  else
    render action: :new
  end
end

#destroyObject


46
47
48
49
50
51
# File 'app/controllers/constructor_pages/fields_controller.rb', line 46

def destroy
  @field = Field.find(params[:id])
  name, template = @field.name, @field.template.id
  @field.destroy
  redirect_to edit_template_url(template), notice: t(:field_success_removed, name: name)
end

#editObject


9
10
11
# File 'app/controllers/constructor_pages/fields_controller.rb', line 9

def edit
  @field = Field.find(params[:id]).tap {|f| @template = f.template = Template.find(params[:template_id])}
end

#newObject


5
6
7
# File 'app/controllers/constructor_pages/fields_controller.rb', line 5

def new
  @field = Field.new.tap {|f| @template = f.template = Template.find(params[:template_id])}
end

#updateObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/constructor_pages/fields_controller.rb', line 24

def update
  @field = Field.find params[:id]
  @template = @field.template

  unless @field.type_value == params[:field][:type_value]
    @field.type_class.where(field_id: @field.id).each do |field|
      "constructor_pages/types/#{params[:field][:type_value]}_type".classify.constantize.new(
          field_id: @field.id, page_id: field.page_id).tap {|f|
        f.value = field.value unless [@field.type_value, params[:field][:type_value]].include?('image') and
                              (@field.type_value == 'string' and field.value.strip == '')
        f.save; field.destroy
      }
    end
  end

  if @field.update field_params
    redirect_to edit_template_path(@template.id), notice: t(:field_success_updated, name: @field.name)
  else
    render action: :edit
  end
end