Class: Dscf::Marketplace::SupplierProductsController

Inherits:
ApplicationController show all
Includes:
Core::Common
Defined in:
app/controllers/dscf/marketplace/supplier_products_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#bypass_permissions_for_demo?, #pundit_user

Instance Method Details

#approveObject

Admin action to approve a supplier product (makes it active)



47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/dscf/marketplace/supplier_products_controller.rb', line 47

def approve
  set_object
  authorize @obj, :approve?

  if @obj.update(status: :active)
    render_success("supplier_products.success.approve", data: @obj)
  else
    render_error("supplier_products.errors.approve", errors: @obj.errors.full_messages, status: :unprocessable_entity)
  end
end

#createObject

Override create so supplier submissions default to inactive (require approval)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/dscf/marketplace/supplier_products_controller.rb', line 20

def create
  authorize @clazz.new, :create?

  obj = @clazz.new(model_params)
  # Ensure newly created supplier products are inactive until approved by platform admin
  obj.status = :inactive

  ActiveRecord::Base.transaction do
    if obj.save
      @obj = obj
      after_save_hook(obj) if respond_to?(:after_save_hook, true)

      obj = @clazz.includes(eager_loaded_associations).find(obj.id) if eager_loaded_associations.present?
      @obj = obj

      includes = serializer_includes_for_action(:create)
      options = {}
      options[:include] = includes if includes.present?

      render_success(data: obj, serializer_options: options, status: :created)
    else
      render_error(errors: obj.errors.full_messages[0], status: :unprocessable_entity)
    end
  end
end

#my_productsObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/dscf/marketplace/supplier_products_controller.rb', line 6

def my_products
  authorize @clazz.new, :my_products?
  service = MyResourceService.new(current_user)
  products = service.my_products(params)

  options = {
    include: default_serializer_includes[:index] || [],
    meta: {resource_type: "my_products"}
  }

  render_success("supplier_products.success.index", data: products, serializer_options: options)
end

#rejectObject

Admin action to reject a supplier product (makes it inactive)



59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/dscf/marketplace/supplier_products_controller.rb', line 59

def reject
  set_object
  authorize @obj, :approve?

  if @obj.update(status: :inactive)
    render_success("supplier_products.success.reject", data: @obj)
  else
    render_error("supplier_products.errors.reject", errors: @obj.errors.full_messages, status: :unprocessable_entity)
  end
end