Guest User

Untitled

a guest
Feb 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. class Policy
  2. class << self
  3. attr_accessor :rename_attributes
  4.  
  5. def rename_attributes(&block)
  6. @root ||= {}
  7. @block = block
  8. end
  9.  
  10. def mapped_attributes
  11. instance_eval(&@block)
  12. @root
  13. end
  14.  
  15. def rename(from:, to:, &attribues_block)
  16. children = instance_eval(&attribues_block) if block_given?
  17. @root[from] = {
  18. to: to,
  19. children: children,
  20. }
  21. @root
  22. end
  23.  
  24. def delegates_renaming_to(policy_klass)
  25. policy_klass.mapped_attributes
  26. end
  27. end
  28. end
  29.  
  30. class StockGroupPolicy < Policy
  31. rename_attributes do
  32. rename from: :batch, to: :stock_group_attributes
  33. rename from: :batch_id, to: :stock_group_id
  34. end
  35. end
  36.  
  37. class StockAdjustmentLineItemPolicy < Policy
  38. rename_attributes do
  39. rename from: :stock_trails, to: :stock_trails_attrs do
  40. delegates_renaming_to StockGroupPolicy
  41. end
  42. end
  43. end
  44.  
  45. class StockAdjustmentPolicy < Policy
  46. rename_attributes do
  47. rename from: :stock_adjustment_line_items, to: :stock_adjustment_line_items_attrs do
  48. delegates_renaming_to StockAdjustmentLineItemPolicy
  49. end
  50. end
  51. end
  52.  
  53.  
  54.  
  55. StockAdjustmentPolicy.mapped_attributes
Add Comment
Please, Sign In to add comment