Class: TypedEAVController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- TypedEAVController
- Includes:
- TypedEAVControllerConcern
- Defined in:
- lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb
Instance Method Summary collapse
-
#add_option ⇒ Object
POST /typed_eav_fields/:typed_eav_field_id/field_options/add_option.
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
-
#remove_option ⇒ Object
DELETE /typed_eav_fields/:typed_eav_field_id/field_options/remove_option.
- #show ⇒ Object
- #update ⇒ Object
Methods included from TypedEAVControllerConcern
Instance Method Details
#add_option ⇒ Object
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..maximum(:sort_order) || 0) + 1 @field..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 |
#create ⇒ Object
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 |
#destroy ⇒ Object
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 |
#edit ⇒ Object
44 |
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 44 def edit; end |
#index ⇒ Object
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 |
#new ⇒ Object
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_option ⇒ Object
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..find(params[:option_id]).destroy! redirect_to edit_typed_eav_field_path(@field), status: :see_other end |
#show ⇒ Object
37 |
# File 'lib/generators/typed_eav/scaffold/templates/controllers/typed_eav_controller.rb', line 37 def show; end |
#update ⇒ Object
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 |