Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Invoice < ActiveRecord::Base
- has_many :invoice_lines
- def readonly?; self.invoice_sent? end
- end
- def InvoiceLine < ActiveRecord::Base
- def readonly?; self.invoice.readonly? end
- end
- def create_or_update
- raise ReadOnlyRecord if readonly?
- ...
- end
- original = line.readonly?
- line.readonly? = lambda: false
- line.save()
- line.readonly? = original
- line = InvoiceLine.last
- def line.readonly?; false; end
- line.update_columns(description: "Compliments cost nothing", amount: 0)
- InvoiceLine.where(description: "Free Stuff Tuesday").update_all(amount: 0)
- class InvoiceLine < ActiveRecord::Base
- attr_accessor :force_writeable
- def readonly?
- invoice.readonly? unless force_writeable
- end
- end
- line.force_writable = true
- line.update(description: "new narrative line")
- class InvoiceLine < ActiveRecord::Base
- def force_update(&block)
- saved_force_update = @_force_update
- @_force_update = true
- result = yield
- @_force_update = saved_force_update
- result
- end
- def readonly?
- invoice.readonly? unless @_force_update
- end
- end
- line.force_update do
- line.update(description: "new description")
- end
- class InvoiceLine < ActiveRecord::Base
- validate :readonly_policy
- def readonly_policy
- if invoice.readonly?
- (changed - ["description", "amount"]).each do |attr|
- errors.add(attr, "is a read-only attribute")
- end
- end
- end
- end
- invoice_line1.instance_variable_set(:@item_num, 123)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement