Class: Binda::FieldGroupsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/binda/field_groups_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#after_sign_in_path_for, #after_sign_out_path_for, #set_locale

Instance Method Details

#add_field_settingObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/binda/field_groups_controller.rb', line 70

def add_field_setting
  # We set some default values in order to be able to save the field setting
  # (if field setting isn't save it makes impossible to sort the order)
  # TODO params should be passed into a permit method
  @field_setting = FieldSetting.new(
    name: "#{I18n.t('binda.field_setting.new')}",
    field_group_id: @field_group.id, 
    field_type: 'string',
    ancestry: params[:ancestry]
  )
  @field_setting.save!
  render 'binda/field_groups/_form_new_item', layout: false
end

#createObject



23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/binda/field_groups_controller.rb', line 23

def create
  @field_group = @structure.field_groups.build(field_group_params)

  if @field_group.save
    reset_field_settings_cache
    redirect_to structure_field_group_path( @structure.slug, @field_group.slug ), notice: 'Field group was successfully created.'
  else
    redirect_to new_structure_field_group_path( @structure.slug ), flash: { alert: @field_group.errors }
  end
end

#destroyObject



48
49
50
51
52
53
54
55
56
# File 'app/controllers/binda/field_groups_controller.rb', line 48

def destroy
  @field_group.destroy!
  reset_field_settings_cache
  if params[:isAjax]
    render json: { target_id: params[:target_id] }, status: 200
  else
    redirect_to structure_path( @structure.slug ), notice: 'Field group was successfully destroyed along with all dependents.'
  end
end

#editObject



20
21
# File 'app/controllers/binda/field_groups_controller.rb', line 20

def edit
end

#indexObject



8
9
10
# File 'app/controllers/binda/field_groups_controller.rb', line 8

def index
  redirect_to structure_field_group_path( @structure.slug )
end

#newObject



16
17
18
# File 'app/controllers/binda/field_groups_controller.rb', line 16

def new
  @field_group = @structure.field_groups.build()
end

#showObject



12
13
14
# File 'app/controllers/binda/field_groups_controller.rb', line 12

def show
  redirect_to action: :edit
end

#sortObject



58
59
60
61
62
63
# File 'app/controllers/binda/field_groups_controller.rb', line 58

def sort
  params[:field_group].each_with_index do |id, i|
    FieldGroup.find( id ).update_column('position', i+1) # use update_column to skip callbacks (which leads to huge useless memory consumption)
  end
  render json: { id: "##{params[:id]}" }, status: 200
end

#sort_field_settingsObject



65
66
67
68
# File 'app/controllers/binda/field_groups_controller.rb', line 65

def sort_field_settings
  sort_field_setting_by(params["form--list-item"])
  render json: { id: "##{params[:id]}" }, status: 200
end

#updateObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/binda/field_groups_controller.rb', line 34

def update
  # Add nested classes
  add_new_choices
  check_if_needs_to_update_choices

  # Update the other ones
  if @field_group.update(field_group_params)
    reset_field_settings_cache
    redirect_to structure_field_group_path( @structure.slug, @field_group.slug ), notice: 'Field group was successfully updated.'
  else
    redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { alert: @field_group.errors }
  end
end