product_item.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # encoding:utf-8
  2. class ProductItem < ActiveRecord::Base
  3. has_paper_trail
  4. self.table_name = "product_items"
  5. validates :product_id,:item_id,:nums, presence: true
  6. validate :product_validation
  7. def product_validation
  8. if self.product_id > 0
  9. prd = Product.where("id = ?", self.product_id).first
  10. if prd.blank?
  11. self.errors.add(:product_id,"商品不存在,请重新填写商品ID")
  12. else
  13. if !prd.package
  14. self.errors.add(:product_id,"商品非套装商品,请重新录入!")
  15. end
  16. self.title=prd.name
  17. end
  18. end
  19. if self.item_id > 0
  20. prd = Product.where("id = ?", self.item_id).first
  21. if prd.blank?
  22. self.errors.add(:item_id,"商品不存在,请填写正确的明细ID")
  23. else
  24. self.item_title=prd.name
  25. end
  26. end
  27. #清空套装缓存
  28. url = "#{CONFIG_FILE["api_host"]}/railsadmin/clean_cache/package/#{self.product_id}"
  29. p url
  30. open(url)
  31. end
  32. rails_admin do
  33. navigation_label '套装商品明细'
  34. weight -300
  35. parent Product
  36. list do
  37. filters [:product_id,:item_id]
  38. # include_all_fields
  39. field :id
  40. field :product_id
  41. field :title
  42. field :item_id
  43. field :item_title
  44. field :nums
  45. end
  46. show do
  47. field :id
  48. field :product_id
  49. field :item_id
  50. field :nums
  51. end
  52. edit do
  53. field :product_id
  54. field :item_id
  55. field :nums
  56. end
  57. end
  58. end