Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. class Invoice < ActiveRecord::Base
  2. has_many :invoice_lines
  3. def readonly?; self.invoice_sent? end
  4. end
  5.  
  6. def InvoiceLine < ActiveRecord::Base
  7. def readonly?; self.invoice.readonly? end
  8. end
  9.  
  10. def create_or_update
  11. raise ReadOnlyRecord if readonly?
  12. ...
  13. end
  14.  
  15. original = line.readonly?
  16. line.readonly? = lambda: false
  17. line.save()
  18. line.readonly? = original
  19.  
  20. line = InvoiceLine.last
  21. def line.readonly?; false; end
  22.  
  23. line.update_columns(description: "Compliments cost nothing", amount: 0)
  24.  
  25. InvoiceLine.where(description: "Free Stuff Tuesday").update_all(amount: 0)
  26.  
  27. class InvoiceLine < ActiveRecord::Base
  28. attr_accessor :force_writeable
  29.  
  30. def readonly?
  31. invoice.readonly? unless force_writeable
  32. end
  33. end
  34.  
  35. line.force_writable = true
  36. line.update(description: "new narrative line")
  37.  
  38. class InvoiceLine < ActiveRecord::Base
  39. def force_update(&block)
  40. saved_force_update = @_force_update
  41. @_force_update = true
  42. result = yield
  43. @_force_update = saved_force_update
  44. result
  45. end
  46.  
  47. def readonly?
  48. invoice.readonly? unless @_force_update
  49. end
  50. end
  51.  
  52. line.force_update do
  53. line.update(description: "new description")
  54. end
  55.  
  56. class InvoiceLine < ActiveRecord::Base
  57. validate :readonly_policy
  58.  
  59. def readonly_policy
  60. if invoice.readonly?
  61. (changed - ["description", "amount"]).each do |attr|
  62. errors.add(attr, "is a read-only attribute")
  63. end
  64. end
  65. end
  66. end
  67.  
  68. invoice_line1.instance_variable_set(:@item_num, 123)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement