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 "\n alias_method :original_update_attributes, :update_attributes\n undef_method :update_attributes\n define_method :update_attributes do |attributes, options = {}, query_options = nil|\n load(attributes) && save(options, query_options)\n end\n\n alias_method :original_save, :save\n undef_method :save\n define_method :save do |options = {}, query_options = nil|\n begin\n perform_validation = case options\n when Hash\n options.delete(:validate) != false\n when NilClass\n true\n else\n options\n end\n\n # clear the remote validations so they don't interfere with the local\n # ones. Otherwise we get an endless loop and can never change the\n # fields so as to make the resource valid\n @remote_errors = nil\n if perform_validation && valid? || !perform_validation\n save_without_validation(options, query_options)\n true\n else\n false\n end\n rescue ActiveResource::ResourceInvalid => error\n # cache the remote errors because every call to <tt>valid?</tt> clears\n # all errors. We must keep a copy to add these back after local\n # validations\n @remote_errors = error\n load_remote_errors(@remote_errors, true)\n false\n end\n end\n\n alias_method :original_save_without_validation, :save_without_validation\n undef_method :save_without_validation\n define_method :save_without_validation do |path_options = {}, query_options = nil|\n new? ? create(path_options, query_options) : update(path_options, query_options)\n end\n\n alias_method :original_create, :create\n undef_method :create\n define_method :create do |path_options = {}, query_options = nil|\n connection.post(create_path(path_options, query_options), encode, self.class.headers).tap do |response|\n self.id = id_from_response(response)\n load_attributes_from_response(response)\n end\n end\n\n alias_method :original_update, :update\n undef_method :update\n define_method :update do |path_options = {}, query_options = nil|\n connection.put(update_path(path_options, query_options), encode, self.class.headers).tap do |response|\n load_attributes_from_response(response)\n end\n end\n\n RUBY\n\n base.send(:include, InstanceMethods)\n base.extend(ClassMethods)\n base.set_default_values\nend\n" |