Permalink
Browse files

0.0.3

  • Loading branch information...
1 parent 023a102 commit ec7bf045a9c7f50f9741bbcd5e6cd3f1efc1f3af @einzige einzige committed Jun 15, 2012
Showing with 46 additions and 49 deletions.
  1. +1 −1 VERSION
  2. +41 −44 lib/sneaky-save.rb
  3. +4 −4 sneaky-save.gemspec
View
@@ -1 +1 @@
-0.0.2
+0.0.3
View
@@ -5,60 +5,57 @@
module SneakySave
extend ActiveSupport::Concern
- module InstanceMethods
+ # Saves record without running callbacks/validations.
+ # Returns true if any record is changed.
+ #
+ # @note - Does not reload updated record by default.
+ # - Does not save associated collections.
+ # - Saves only belongs_to relations.
+ #
+ # @return [false, true]
+ def sneaky_save
+ begin
+ new_record? ? sneaky_create : sneaky_update
+ rescue ActiveRecord::StatementInvalid
+ false
+ end
+ end
- # Saves record without running callbacks/validations.
- # Returns true if any record is changed.
+ protected
+
+ # Makes INSERT query in database without running any callbacks.
#
- # @note - Does not reload updated record by default.
- # - Does not save associated collections.
- # - Saves only belongs_to relations.
- #
# @return [false, true]
- def sneaky_save
- begin
- new_record? ? sneaky_create : sneaky_update
- rescue ActiveRecord::StatementInvalid
- false
+ def sneaky_create
+ if self.id.nil? && connection.prefetch_primary_key?(self.class.table_name)
+ self.id = connection.next_sequence_value(self.class.sequence_name)
end
- end
-
- protected
-
- # Makes INSERT query in database without running any callbacks.
- #
- # @return [false, true]
- def sneaky_create
- if self.id.nil? && connection.prefetch_primary_key?(self.class.table_name)
- self.id = connection.next_sequence_value(self.class.sequence_name)
- end
-
- attributes_values = send :arel_attributes_values
- new_id = if attributes_values.empty?
- self.class.unscoped.insert connection.empty_insert_statement_value
- else
- self.class.unscoped.insert attributes_values
- end
+ attributes_values = send :arel_attributes_values
- @new_record = false
- !!(self.id ||= new_id)
+ new_id = if attributes_values.empty?
+ self.class.unscoped.insert connection.empty_insert_statement_value
+ else
+ self.class.unscoped.insert attributes_values
end
- # Makes update query without running callbacks.
- #
- # @return [false, true]
- def sneaky_update
+ @new_record = false
+ !!(self.id ||= new_id)
+ end
- # Handle no changes.
- return true unless changes.any?
+ # Makes update query without running callbacks.
+ #
+ # @return [false, true]
+ def sneaky_update
- # Here we have changes --> save them.
- pk = self.class.primary_key
- original_id = changed_attributes.has_key?(pk) ? changes[pk].first : send(pk)
- !self.class.update_all(attributes, pk => original_id).zero?
- end
- end
+ # Handle no changes.
+ return true unless changes.any?
+
+ # Here we have changes --> save them.
+ pk = self.class.primary_key
+ original_id = changed_attributes.has_key?(pk) ? changes[pk].first : send(pk)
+ !self.class.update_all(attributes, pk => original_id).zero?
+ end
end
ActiveRecord::Base.send :include, SneakySave
View
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = %q{sneaky-save}
- s.version = "0.0.2"
+ s.version = "0.0.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Sergei Zinin", "Kevin Goslar"]
@@ -24,16 +24,16 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<activerecord>, [">= 2.3.2"])
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.2.0"])
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
else
- s.add_dependency(%q<activerecord>, [">= 2.3.2"])
+ s.add_dependency(%q<activerecord>, [">= 3.2.0"])
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
end
else
- s.add_dependency(%q<activerecord>, [">= 2.3.2"])
+ s.add_dependency(%q<activerecord>, [">= 3.2.0"])
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
end

0 comments on commit ec7bf04

Please sign in to comment.