Class: RivetCms::FieldsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/rivet_cms/fields_controller.rb', line 16

def create
  field = @owner.fields.build(field_params)
  field.organization = @owner.organization

  if field.save
    audit "field.created", field
    redirect_to owner_path, notice: "Field created"
  else
    redirect_to owner_path, inertia: { errors: field.errors }
  end
end

#destroyObject



37
38
39
40
41
# File 'app/controllers/rivet_cms/fields_controller.rb', line 37

def destroy
  @field.discard
  audit "field.removed", @field
  redirect_to owner_path, notice: "Field removed"
end

#pairObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/rivet_cms/fields_controller.rb', line 66

def pair
  other_field = @owner.fields.find(params[:pair_with])
  # Pairing mutates both fields, so both must pass the record phase
  authorize! :write, :schema, record: other_field

  # Both fields must be half-width and not already paired
  if @field.width_half? && other_field.width_half? && !@field.paired? && !other_field.paired?
    @field.pair_with!(other_field)
    audit "field.updated", @field, change: "layout"
  end

  redirect_to owner_path
end

#toggle_widthObject



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

def toggle_width
  new_width = @field.width_full? ? "half" : "full"

  @field.update!(width: new_width)

  # If changed to full width, ensure it's on its own row
  if new_width == "full"
    other_fields_on_row = @owner.fields.kept.where(row: @field.row).where.not(id: @field.id)
    if other_fields_on_row.exists?
      @field.move_to_own_row!
    end
  end

  audit "field.updated", @field, change: "layout"
  redirect_to owner_path
end

#unpairObject



60
61
62
63
64
# File 'app/controllers/rivet_cms/fields_controller.rb', line 60

def unpair
  @field.unpair!
  audit "field.updated", @field, change: "layout"
  redirect_to owner_path
end

#updateObject



28
29
30
31
32
33
34
35
# File 'app/controllers/rivet_cms/fields_controller.rb', line 28

def update
  if @field.update(field_params)
    audit "field.updated", @field
    redirect_to owner_path, notice: "Field updated"
  else
    redirect_to owner_path, inertia: { errors: @field.errors }
  end
end

#update_layoutObject



80
81
82
83
84
85
86
87
88
# File 'app/controllers/rivet_cms/fields_controller.rb', line 80

def update_layout
  # The bulk update touches every referenced field, so each must pass the
  # record phase, not just the owner
  moved = @owner.fields.where(id: Array(params[:rows]).flatten)
  moved.each { |field| authorize! :write, :schema, record: field }
  @owner.fields.update_layout!(params[:rows])
  audit "schema.layout_updated", @owner
  redirect_to owner_path
end