Class: Spree::Admin::ExportsController

Inherits:
ResourceController
  • Object
show all
Includes:
ActiveStorage::SetCurrent
Defined in:
app/controllers/spree/admin/exports_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

Allow raw JSON attributes for STI and metadata Example:

type: 'SpreeCmCommissioner::Exports::OperatorGuestJsonGzip',
public_metadata: {...,
exportable_type: 'Spree::Taxon',
exportable_id: 1

}



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
58
59
60
61
62
63
64
65
66
# File 'app/controllers/spree/admin/exports_controller.rb', line 30

def create # rubocop:disable Metrics/AbcSize
  attrs = permitted_resource_params.to_h.with_indifferent_access

  # Parse public_metadata JSON
  if attrs[:public_metadata].present?
    begin
      attrs[:public_metadata] = JSON.parse(attrs[:public_metadata]) if attrs[:public_metadata].is_a?(String)
    rescue JSON::ParserError => e
      flash[:error] = "Invalid JSON in public_metadata: #{e.message}"
      return redirect_to new_admin_export_path
    end
  end

  # Parse private_metadata JSON
  if attrs[:private_metadata].present?
    begin
      attrs[:private_metadata] = JSON.parse(attrs[:private_metadata]) if attrs[:private_metadata].is_a?(String)
    rescue JSON::ParserError => e
      flash[:error] = "Invalid JSON in private_metadata: #{e.message}"
      return redirect_to new_admin_export_path
    end
  end

  export_class = attrs[:type].constantize
  export = export_class.new(attrs.except(:type))

  if export.save
    flash[:success] = flash_message_for(export, :successfully_created)
  else
    flash[:error] = export.errors.full_messages.join(', ')
  end

  redirect_to admin_exports_path
rescue NameError => e
  flash[:error] = "Invalid export type: #{e.message}"
  redirect_to admin_exports_path
end

#destroyObject



68
69
70
71
72
73
74
75
# File 'app/controllers/spree/admin/exports_controller.rb', line 68

def destroy
  if @export.destroy
    flash[:success] = flash_message_for(@export, :successfully_removed)
  else
    flash[:error] = @export.errors.full_messages.join(', ')
  end
  redirect_to admin_exports_path
end

#indexObject



8
9
10
11
12
13
# File 'app/controllers/spree/admin/exports_controller.rb', line 8

def index
  @exports = SpreeCmCommissioner::Export
             .order(created_at: :desc)
             .page(params[:page])
             .per(params[:per_page] || 25)
end

#newObject



19
20
21
# File 'app/controllers/spree/admin/exports_controller.rb', line 19

def new
  @export = SpreeCmCommissioner::Export.new
end

#showObject



15
16
17
# File 'app/controllers/spree/admin/exports_controller.rb', line 15

def show
  # @export is loaded by before_action
end