Class: Spree::Admin::ResourceController

Inherits:
BaseController
  • Object
show all
Includes:
Pagy::Method, Callbacks, TableConcern
Defined in:
app/controllers/spree/admin/resource_controller.rb

Direct Known Subclasses

AddressesController, AllowedOriginsController, ApiKeysController, AssetsController, ChannelsController, CheckoutsController, ClassificationsController, CouponCodesController, CustomerGroupUsersController, CustomerGroupsController, CustomerReturnsController, DigitalAssetsController, ExportsController, GiftCardBatchesController, GiftCardsController, ImportMappingsController, ImportRowsController, ImportsController, IntegrationsController, JsonPreviewsController, LineItemsController, MarketsController, MetafieldDefinitionsController, MetafieldsController, NewsletterSubscribersController, OptionTypesController, OptionValuesController, Orders::CustomerReturnsController, Orders::ReturnAuthorizationsController, OrdersController, PaymentMethodsController, PaymentsController, PoliciesController, PriceListProductsController, PriceListsController, PriceRulesController, ProductsController, PromotionActionsController, PromotionRulesController, PromotionsController, RefundReasonsController, RefundsController, ReimbursementTypesController, ReimbursementsController, ReportsController, ReturnAuthorizationReasonsController, ReturnAuthorizationsController, ReturnItemsController, RolesController, ShipmentsController, ShippingCategoriesController, ShippingMethodsController, StatesController, StockItemsController, StockLocationsController, StockMovementsController, StockTransfersController, StoreCreditCategoriesController, StoreCreditsController, TaxCategoriesController, TaxRatesController, TaxonomiesController, TaxonsController, TranslationsController, UsersController, VariantsController, WebhookDeliveriesController, WebhookEndpointsController, ZonesController

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TableConcern

#apply_table_sort, #custom_sort_active?, #process_table_query_state, #table, #table_key, #table_registered?

Methods included from BreadcrumbConcern

#add_breadcrumb_icon_instance_var

Class Attribute Details

.parent_dataObject

Returns the value of attribute parent_data.



126
127
128
# File 'app/controllers/spree/admin/resource_controller.rb', line 126

def parent_data
  @parent_data
end

Class Method Details

.belongs_to(model_name, options = {}) ⇒ Object



128
129
130
131
132
133
# File 'app/controllers/spree/admin/resource_controller.rb', line 128

def belongs_to(model_name, options = {})
  @parent_data ||= {}
  @parent_data[:model_name] = model_name
  @parent_data[:model_class] = model_name.to_s.classify.constantize
  @parent_data[:find_by] = options[:find_by] || :prefix_id
end

Instance Method Details

#createObject

POST /admin/<resource_name_plural>



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/spree/admin/resource_controller.rb', line 60

def create
  invoke_callbacks(:create, :before)
  set_created_by
  @object.attributes = permitted_resource_params
  if @object.save
    invoke_callbacks(:create, :after)
    respond_to do |format|
      if create_turbo_stream_enabled?
        format.turbo_stream do
          flash.now[:success] = message_after_create
        end
      end
      format.html do
        flash[:success] = message_after_create
        redirect_to location_after_create, status: :see_other
      end
    end
  else
    invoke_callbacks(:create, :fails)
    respond_to do |format|
      if create_turbo_stream_enabled?
        format.turbo_stream do
          flash.now[:error] = @object.errors.full_messages.join(', ')
        end
      end
      format.html { render action: :new, status: :unprocessable_content }
    end
  end
end

#destroyObject

DELETE /admin/<resource_name_plural>/<id>



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/spree/admin/resource_controller.rb', line 91

def destroy
  invoke_callbacks(:destroy, :before)
  if @object.destroy
    invoke_callbacks(:destroy, :after)

    respond_to do |format|
      if destroy_turbo_stream_enabled?
        format.turbo_stream do
          flash.now[:success] = flash_message_for(@object, :successfully_removed)
        end
      end
      format.html do
        flash[:success] = flash_message_for(@object, :successfully_removed)
        redirect_to location_after_destroy, status: :see_other
      end
    end
  else
    invoke_callbacks(:destroy, :fails)

    respond_to do |format|
      if destroy_turbo_stream_enabled?
        format.turbo_stream do
          flash.now[:error] = @object.errors.full_messages.to_sentence
        end
      end
      format.html do
        flash[:error] = @object.errors.full_messages.to_sentence
      end
    end
  end
end

#editObject

GET /admin/<resource_name_plural>/<id>/edit



23
24
25
# File 'app/controllers/spree/admin/resource_controller.rb', line 23

def edit
  invoke_callbacks(:edit_action, :before)
end

#indexObject



13
14
15
# File 'app/controllers/spree/admin/resource_controller.rb', line 13

def index
  @collection = collection
end

#newObject

GET /admin/<resource_name>/new



18
19
20
# File 'app/controllers/spree/admin/resource_controller.rb', line 18

def new
  invoke_callbacks(:new_action, :before)
end

#updateObject

PUT /admin/<resource_name_plural>/<id>



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/spree/admin/resource_controller.rb', line 28

def update
  invoke_callbacks(:update, :before)
  if @object.update(permitted_resource_params)
    set_current_store
    remove_assets(%w[asset image square_image])

    invoke_callbacks(:update, :after)
    respond_to do |format|
      if update_turbo_stream_enabled?
        format.turbo_stream do
          flash.now[:success] = message_after_update
        end
      end
      format.html do
        flash[:success] = message_after_update
        redirect_to location_after_save, status: :see_other
      end
    end
  else
    invoke_callbacks(:update, :fails)
    respond_to do |format|
      if update_turbo_stream_enabled?
        format.turbo_stream do
          flash.now[:error] = @object.errors.full_messages.join(', ')
        end
      end
      format.html { render action: :edit, status: :unprocessable_content }
    end
  end
end