Class: Api::V1::ProductTypesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v1/product_types_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



54
55
56
57
58
59
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
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/api/v1/product_types_controller.rb', line 54

def create
  begin
    ActiveRecord::Base.transaction do
      product_type = ProductType.new
      product_type.description = params[:description]
      product_type.sku = params[:sku]
      product_type.unit_of_measurement_id = params[:unit_of_measurement]
      product_type.comment = params[:comment]

      product_type.created_by_party = current_user.party

      product_type.save!

      #
      # For scoping by party, add party_id and role_type 'vendor' to product_party_roles table. However may want to override controller elsewhere
      # so that default is no scoping in erp_products engine
      #
      party_role = params[:party_role]
      party_id = params[:party_id]
      unless party_role.blank? or party_id.blank?
        product_type_party_role = ProductTypePtyRole.new
        product_type_party_role.product_type = product_type
        product_type_party_role.party_id = party_id
        product_type_party_role.role_type = RoleType.iid(party_role)
        product_type_party_role.save
      end
    end

    render :json => {success: true,
                     product_type: product_type.to_data_hash}

  rescue ActiveRecord::RecordInvalid => invalid

    render :json => {success: false, message: invalid.record.errors.messages}

  rescue => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    # email error
    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render :json => {success: false, message: 'Could not create product type'}
  end
end

#destroyObject



132
133
134
135
136
# File 'app/controllers/api/v1/product_types_controller.rb', line 132

def destroy
  ProductType.find(params[:id]).destroy

  render :json => {:success => true}
end

#indexObject



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
# File 'app/controllers/api/v1/product_types_controller.rb', line 5

def index
  sort = nil
  dir = nil
  limit = nil
  start = nil

  unless params[:sort].blank?
    sort_hash = params[:sort].blank? ? {} : Hash.symbolize_keys(JSON.parse(params[:sort]).first)
    sort = sort_hash[:property] || 'description'
    dir = sort_hash[:direction] || 'ASC'
    limit = params[:limit] || 25
    start = params[:start] || 0
  end

  query_filter = params[:query_filter].blank? ? {} : JSON.parse(params[:query_filter]).symbolize_keys

  # hook method to apply any scopes passed via parameters to this api
  product_types = ProductType.apply_filters(query_filter)

  # scope by dba_organizations if there are no parties passed as filters
  unless query_filter[:parties]
    dba_organizations = [current_user.party.dba_organization]
    dba_organizations = dba_organizations.concat(current_user.party.dba_organization.child_dba_organizations)
    product_types = product_types.scope_by_dba_organization(dba_organizations)
  end

  if sort and dir
    product_types = product_types.order("#{sort} #{dir}")
  end

  total_count = product_types.count

  if start and limit
    product_types = product_types.offset(start).limit(limit)
  end

  render :json => {success: true,
                   total_count: total_count,
                   product_types: product_types.collect { |product_type| product_type.to_data_hash }}

end

#showObject



47
48
49
50
51
52
# File 'app/controllers/api/v1/product_types_controller.rb', line 47

def show
  product_type = ProductType.find(params[:id])

  render :json => {success: true,
                   product_type: product_type.to_data_hash}
end

#updateObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/api/v1/product_types_controller.rb', line 100

def update
  begin
    ActiveRecord::Base.transaction do
      product_type = ProductType.find(params[:id])

      product_type.description = params[:description]
      product_type.sku = params[:sku]
      product_type.unit_of_measurement_id = params[:unit_of_measurement]
      product_type.comment = params[:comment]

      product_type.updated_by_party = current_user.party

      product_type.save!

      render :json => {success: true,
                       product_type: product_type.to_data_hash}
    end
  rescue ActiveRecord::RecordInvalid => invalid

    render :json => {success: false, message: invalid.record.errors.messages}

  rescue => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    # email error
    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render :json => {success: false, message: 'Could not update product type'}
  end
end