Class: TypedEAVController

Inherits:
ApplicationController
  • Object
show all
Includes:
TypedEAVControllerConcern
Defined in:
lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb

Instance Method Summary collapse

Methods included from TypedEAVControllerConcern

#typed_eav_filter_params

Instance Method Details

#add_optionObject

POST /typed_eav_fields/:typed_eav_field_id/field_options/add_option



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 88

def add_option
  @field = scoped_fields.find(params[:typed_eav_field_id])
  # Lock the field row during option creation so two concurrent creates
  # can't observe the same `count` and assign the same sort_order.
  @field.with_lock do
    next_order = (@field.field_options.maximum(:sort_order) || 0) + 1
    @field.field_options.create!(
      label: params[:option_label],
      value: params[:option_value],
      sort_order: next_order
    )
  end
  redirect_to edit_typed_eav_field_path(@field), status: :see_other
end

#createObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 46

def create
  type_class = resolve_type_class(params.dig(:typed_eav_field, :field_type) || params[:type])
  ambient = ensure_scope!
  attrs = field_params(type_class, creating: true)
  attrs[:scope] = ambient
  attrs[:section_id] = verified_section_id(
    params.dig(:typed_eav_field, :section_id),
    attrs[:entity_type],
    ambient
  )
  @field = type_class.new(attrs)

  if @field.save
    redirect_to edit_typed_eav_field_path(@field), status: :see_other,
      notice: "Field created."
  else
    render :new, status: :unprocessable_content
  end
end

#destroyObject



82
83
84
85
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 82

def destroy
  @field.destroy!
  redirect_to typed_eav_fields_path, status: :see_other, notice: "Field deleted."
end

#editObject



44
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 44

def edit; end

#indexObject



33
34
35
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 33

def index
  @fields = scoped_fields.order(:entity_type, :scope, :sort_order, :name)
end

#newObject



39
40
41
42
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 39

def new
  type_class = resolve_type_class(params[:type])
  @field = type_class.new
end

#remove_optionObject

DELETE /typed_eav_fields/:typed_eav_field_id/field_options/remove_option



104
105
106
107
108
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 104

def remove_option
  @field = scoped_fields.find(params[:typed_eav_field_id])
  @field.field_options.find(params[:option_id]).destroy!
  redirect_to edit_typed_eav_field_path(@field), status: :see_other
end

#showObject



37
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 37

def show; end

#updateObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 66

def update
  attrs = field_params(@field.class, creating: false)
  attrs[:section_id] = verified_section_id(
    params.dig(:typed_eav_field, :section_id),
    @field.entity_type,
    @field.scope
  )

  if @field.update(attrs)
    redirect_to edit_typed_eav_field_path(@field), status: :see_other,
      notice: "Field updated."
  else
    render :edit, status: :unprocessable_content
  end
end