cirandas.net

ref: master

plugins/suppliers/controllers/myprofile/suppliers_plugin/basket_controller.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class SuppliersPlugin::BasketController < MyProfileController

  include SuppliersPlugin::TranslationHelper

  no_design_blocks

  protect 'edit_profile', :profile
  before_filter :set_allowed_user

  helper SuppliersPlugin::TranslationHelper
  helper SuppliersPlugin::DisplayHelper

  def search
    @product = profile.products.supplied.find params[:id]
    @query = params[:query].to_s
    @scope = profile.products.supplied.limit(10)
    @scope = @scope.where('id NOT IN (?)', @product.id)
    # not a good option as need to search on from_products to, solr is a perfect match
    #@products = @scope.where('name ILIKE ? OR name ILIKE ?', "#{@query}%", "% #{@query}%")
    @products = autocomplete(:catalog, @scope, @query, {per_page: 10, page: 1}, {})[:results]

    render json: @products.map{ |p|
      {value: p.id, label: "#{p.name} (#{if p.respond_to? :supplier then p.supplier.name else p.profile.short_name end})"}
    }
  end

  def add
    @product = profile.products.supplied.find params[:id]
    @aggregate = profile.products.supplied.find params[:aggregate_id]

    @sp = @product.sources_from_products.where(from_product_id: @aggregate.id).first
    if @sp
      @sp.update_column :quantity, @sp.quantity + 1
    else
      @sp = @product.sources_from_products.create! from_product: @aggregate, to_product: @product
    end

    render partial: 'suppliers_plugin/manage_products/basket_tab'
  end

  def remove
    @product = profile.products.supplied.find params[:id]
    @aggregate = profile.products.supplied.find params[:aggregate_id]
    @sp = @product.sources_from_products.where(from_product_id: @aggregate.id).first
    @sp.destroy

    render partial: 'suppliers_plugin/manage_products/basket_tab'
  end

  protected

  extend HMVC::ClassMethods
  hmvc SuppliersPlugin

  # inherit routes from core skipping use_relative_controller!
  def url_for options
    options[:controller] = "/#{options[:controller]}" if options.is_a? Hash and options[:controller] and not options[:controller].to_s.starts_with? '/'
    super options
  end
  helper_method :url_for

  def set_allowed_user
    @allowed_user = true
  end

end