Module: Oare::Resource
- Defined in:
- lib/active_resource_override/base.rb
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_resource_override/base.rb', line 2 def self.included(base) base.instance_eval <<-RUBY alias_method :original_update_attributes, :update_attributes undef_method :update_attributes define_method :update_attributes do |attributes, options = {}, query_options = nil| load(attributes) && save(options, query_options) end alias_method :original_save, :save undef_method :save define_method :save do |options = {}, query_options = nil| begin perform_validation = case options when Hash options.delete(:validate) != false when NilClass true else options end # clear the remote validations so they don't interfere with the local # ones. Otherwise we get an endless loop and can never change the # fields so as to make the resource valid @remote_errors = nil if perform_validation && valid? || !perform_validation save_without_validation(options, query_options) true else false end rescue ActiveResource::ResourceInvalid => error # cache the remote errors because every call to <tt>valid?</tt> clears # all errors. We must keep a copy to add these back after local # validations @remote_errors = error load_remote_errors(@remote_errors, true) false end end alias_method :original_save_without_validation, :save_without_validation undef_method :save_without_validation define_method :save_without_validation do |path_options = {}, query_options = nil| new? ? create(path_options, query_options) : update(path_options, query_options) end alias_method :original_create, :create undef_method :create define_method :create do |path_options = {}, query_options = nil| connection.post(create_path(path_options, query_options), encode, self.class.headers).tap do |response| self.id = id_from_response(response) load_attributes_from_response(response) end end alias_method :original_update, :update undef_method :update define_method :update do |path_options = {}, query_options = nil| connection.put(update_path(path_options, query_options), encode, self.class.headers).tap do |response| load_attributes_from_response(response) end end RUBY base.send(:include, InstanceMethods) base.extend(ClassMethods) base.set_default_values end |