Guest User

Untitled

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