Class: Binda::ComponentsController

Inherits:
ApplicationController show all
Includes:
FieldableHelpers
Defined in:
app/controllers/binda/components_controller.rb

Instance Method Summary collapse

Methods included from FieldableHelpers

#bytes_to_megabytes, #fieldable_params, #nested_fieldable_params, #return_audio_details, #return_image_details, #return_svg_details, #return_video_details, #upload_details, #upload_params

Methods inherited from ApplicationController

#after_sign_in_path_for, #after_sign_out_path_for, #set_locale

Instance Method Details

#createObject



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

def create
  @component = @structure.components.build(component_params)

  if @component.save
    redirect_to structure_component_path(@structure.slug, @component.slug), notice: "#{ @structure.name } was successfully created."
  else
    @instance = @component
    render :edit, flash: { alert: @component.errors }
  end
end

#destroyObject



53
54
55
56
# File 'app/controllers/binda/components_controller.rb', line 53

def destroy
  @component.destroy
  redirect_to structure_components_url(@structure.slug), notice: "#{ @structure.name.capitalize } was successfully destroyed."
end

#editObject



31
32
# File 'app/controllers/binda/components_controller.rb', line 31

def edit
end

#indexObject



11
12
13
14
15
16
17
18
19
# File 'app/controllers/binda/components_controller.rb', line 11

def index
  # set default value
  order = 'LOWER(name) ASC'
  unless params[:order].nil?
    order_hash = params[:order].permit(:name, :publish_state).to_h 
    order = order_hash.map{|k,v| "LOWER(#{k}) #{v}"}.join(', ') if order_hash.any?
  end
  @components = @structure.components.order(order).all.page params[:page]
end

#newObject



25
26
27
28
29
# File 'app/controllers/binda/components_controller.rb', line 25

def new
  @component = @structure.components.build()
  # The following variable will be used as wildcard by fieldable views
  @instance = @component
end

#new_repeaterObject



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

def new_repeater
  @repeater_setting = FieldSetting.find(params[:repeater_setting_id])
  @repeater = @instance.repeaters.create!(field_setting: @repeater_setting)
  render 'binda/fieldable/_form_item_new_repeater', layout: false
end

#showObject



21
22
23
# File 'app/controllers/binda/components_controller.rb', line 21

def show
  redirect_to action: :edit
end

#sortObject



69
70
71
72
73
74
# File 'app/controllers/binda/components_controller.rb', line 69

def sort
  params[:component].each_with_index do |id, i|
    Component.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_indexObject



76
77
78
79
# File 'app/controllers/binda/components_controller.rb', line 76

def sort_index
  return redirect_to structure_components_path, alert: "There are too many #{@structure.name.pluralize}. It's not possible to sort more than #{Component.sort_limit} #{@structure.name.pluralize}." if @structure.components.length > Component.sort_limit
  @components = @structure.components.order('position').all
end

#sort_repeatersObject



64
65
66
67
# File 'app/controllers/binda/components_controller.rb', line 64

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

#updateObject



45
46
47
48
49
50
51
# File 'app/controllers/binda/components_controller.rb', line 45

def update
  if @component.update(component_params)
    redirect_to structure_component_path(@structure.slug, @component.slug), notice: "#{ @structure.name.capitalize } was successfully updated."
  else
    render :edit, flash: { alert: @component.errors }
  end
end

#uploadObject



81
82
83
84
85
86
87
# File 'app/controllers/binda/components_controller.rb', line 81

def upload
  if @component.update(upload_params(:component))
    render json: upload_details, status: 200
  else
    render json: @component.errors.full_messages, status: 400
  end
end