-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update sneaky_create to update internal rails 5 structure correctly #19
base: master
Are you sure you want to change the base?
Conversation
lib/sneaky-save.rb
Outdated
copy_internal(data, self, "@new_record") | ||
copy_internal(data, self, "@destroyed") | ||
|
||
!!self.id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantSelf: Redundant self detected.
lib/sneaky-save.rb
Outdated
dynamic_keys = sneaky_attributes_without_id.keys.map { |key| ":#{key.name}" }.join(', ') | ||
mapping = sneaky_attributes_without_id.map { |key, value| [jey.name.to_sym, value]}.to_h | ||
table = self.class.table_name | ||
sql = "INSERT INTO #{table} (#{column_keys}) VALUES (#{dynamic_keys}) RETURNING *" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [86/80]
lib/sneaky-save.rb
Outdated
sneaky_attributes_without_id = self.sneaky_attributes_values.except { |key| key.name == 'id' } | ||
column_keys = sneaky_attributes_without_id.keys.map(&:name) | ||
dynamic_keys = sneaky_attributes_without_id.keys.map { |key| ":#{key.name}" }.join(', ') | ||
mapping = sneaky_attributes_without_id.map { |key, value| [jey.name.to_sym, value]}.to_h |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lint/UnusedBlockArgument: Unused block argument - key. If it's necessary, use _ or _key as an argument name to indicate that it won't be used.
Metrics/LineLength: Line is too long. [92/80]
Layout/SpaceInsideBlockBraces: Space missing inside }.
lib/sneaky-save.rb
Outdated
!!(self.id ||= new_id) | ||
sneaky_attributes_without_id = self.sneaky_attributes_values.except { |key| key.name == 'id' } | ||
column_keys = sneaky_attributes_without_id.keys.map(&:name) | ||
dynamic_keys = sneaky_attributes_without_id.keys.map { |key| ":#{key.name}" }.join(', ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [92/80]
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
lib/sneaky-save.rb
Outdated
|
||
@new_record = false | ||
!!(self.id ||= new_id) | ||
sneaky_attributes_without_id = self.sneaky_attributes_values.except { |key| key.name == 'id' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantSelf: Redundant self detected.
Metrics/LineLength: Line is too long. [98/80]
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
lib/sneaky-save.rb
Outdated
if id.nil? && !prefetch_pk_allowed | ||
attributes_values.reject! { |key, _| key.name == 'id' } | ||
end | ||
sql = <<~SQL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/ExtraSpacing: Unnecessary spacing detected.
Layout/SpaceAroundOperators: Operator = should be surrounded by a single space.
lib/sneaky-save.rb
Outdated
attributes_values = sneaky_attributes_values | ||
mapping = sneaky_attributes_without_id | ||
.map { |key, value| [key.name.to_sym, value]} | ||
.to_h |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .to_h with sneaky_attributes_without_id on line 47.
lib/sneaky-save.rb
Outdated
|
||
attributes_values = sneaky_attributes_values | ||
mapping = sneaky_attributes_without_id | ||
.map { |key, value| [key.name.to_sym, value]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .map with sneaky_attributes_without_id on line 47.
Layout/SpaceInsideBlockBraces: Space missing inside }.
lib/sneaky-save.rb
Outdated
column_keys = sneaky_attributes_without_id.keys.map(&:name) | ||
dynamic_keys = sneaky_attributes_without_id.keys | ||
.map { |key| ":#{key.name}" } | ||
.join(', ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .join with .keys on line 43.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
lib/sneaky-save.rb
Outdated
end | ||
column_keys = sneaky_attributes_without_id.keys.map(&:name) | ||
dynamic_keys = sneaky_attributes_without_id.keys | ||
.map { |key| ":#{key.name}" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .map with .keys on line 43.
lib/sneaky-save.rb
Outdated
@@ -36,28 +36,33 @@ def sneaky_create_or_update | |||
# Performs INSERT query without running any callbacks | |||
# @return [false, true] | |||
def sneaky_create | |||
prefetch_pk_allowed = sneaky_connection.prefetch_primary_key?(self.class.table_name) | |||
sneaky_attributes_without_id = sneaky_attributes_values | |||
.except { |key| key.name == 'id' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .except with sneaky_attributes_values on line 39.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
3b08060
to
08d34fd
Compare
lib/sneaky-save.rb
Outdated
if definition.able_to_type_cast? | ||
[definition.name.to_sym, definition.type_cast_for_database(value)] | ||
else | ||
[definition.name.to_sym, value] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/IndentationWidth: Use 2 (not 1) spaces for indentation.
lib/sneaky-save.rb
Outdated
|
||
mapping = sneaky_attributes_without_id.map do |definition, value| | ||
if definition.able_to_type_cast? | ||
[definition.name.to_sym, definition.type_cast_for_database(value)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/IndentationWidth: Use 2 (not 1) spaces for indentation.
if id.nil? && prefetch_pk_allowed | ||
self.id = sneaky_connection.next_sequence_value(self.class.sequence_name) | ||
sneaky_attributes_without_id = sneaky_attributes_values | ||
.except { |key| key.name == "id" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
lib/sneaky-save.rb
Outdated
if result.respond_to?(:encoder) | ||
[definition.name.to_sym, result.encoder.encode(value)] | ||
else | ||
raise RuntimeError.new('Unknown Type Casted Struct') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantException: Redundant RuntimeError.new call can be replaced with just the message.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
lib/sneaky-save.rb
Outdated
.join(", ") | ||
|
||
constraint = if avoid_insert_conflict.present? | ||
"ON CONFLICT (#{[avoid_insert_conflict].flatten.join(', ')}) "\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [82/80]
column_keys = sneaky_attributes_without_id.keys.map(&:name).join(", ") | ||
dynamic_keys = sneaky_attributes_without_id.keys | ||
.map { |key| ":#{key.name}" } | ||
.join(", ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .join with .keys on line 43.
|
||
column_keys = sneaky_attributes_without_id.keys.map(&:name).join(", ") | ||
dynamic_keys = sneaky_attributes_without_id.keys | ||
.map { |key| ":#{key.name}" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .map with .keys on line 43.
!!(self.id ||= new_id) | ||
def sneaky_create(avoid_insert_conflict: nil) | ||
sneaky_attributes_without_id = sneaky_attributes_values | ||
.except { |key| key.name == "id" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
def sneaky_create_or_update | ||
new_record? ? sneaky_create : sneaky_update | ||
def sneaky_create_or_update(avoid_insert_conflict: nil) | ||
new_record? ? sneaky_create(avoid_insert_conflict: avoid_insert_conflict) : sneaky_update |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [93/80]
.join(", ") | ||
dynamic_keys = sneaky_attributes_without_id.keys | ||
.map { |key| ":#{key.name}" } | ||
.join(", ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .join with .keys on line 45.
.map { |key| "\"#{key}\"" } # to avoid conflicts with column names | ||
.join(", ") | ||
dynamic_keys = sneaky_attributes_without_id.keys | ||
.map { |key| ":#{key.name}" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .map with .keys on line 45.
|
||
column_keys = sneaky_attributes_without_id.keys | ||
.map { |key| "\"#{key}\"" } # to avoid conflicts with column names | ||
.join(", ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .join with .keys on line 42.
lib/sneaky-save.rb
Outdated
.except { |key| key.name == "id" } | ||
|
||
column_keys = sneaky_attributes_without_id.keys | ||
.map { |key| "\"#{key}\"" } # to avoid conflicts with column names |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .map with .keys on line 42.
Metrics/LineLength: Line is too long. [84/80]
|
||
dynamic_keys = sneaky_attributes_without_id.keys | ||
.map { |key| ":#{key.name}" } | ||
.join(", ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .join with .keys on line 46.
.join(", ") | ||
|
||
dynamic_keys = sneaky_attributes_without_id.keys | ||
.map { |key| ":#{key.name}" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .map with .keys on line 46.
.except { |key| key.name == "id" } | ||
|
||
column_keys = sneaky_attributes_without_id.keys | ||
.map { |key| "\"#{key.name}\"" } # to avoid conflicts with column names |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/DotPosition: Place the . on the previous line, together with the method call receiver.
Layout/MultilineMethodCallIndentation: Align .map with .keys on line 42.
Metrics/LineLength: Line is too long. [89/80]
lib/sneaky-save.rb
Outdated
target.instance_variable_set(key, source.instance_variable_get(key)) | ||
end | ||
|
||
def generate_constraint(avoid_insert_conflict, column_keys, column_keys) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lint/Syntax: duplicate argument name
There is another problem with |
lib/sneaky-save.rb
Outdated
# by manipulating ranges so that they are not seen as iterable and will be properly handled by | ||
# ActiveRecord::Sanitization.quote_bound_value | ||
# https://github.com/rails/rails/issues/36682 | ||
value.instance_eval('undef :map') if value.is_a?(Range) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/EvalWithLocation: Pass FILE and LINE to eval method, as they are used by backtraces.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
end | ||
end | ||
|
||
def quote(value) | ||
# The built-in Sanitization of Rails does not handle time ranges very well. We work around this | ||
# by manipulating ranges so that they are not seen as iterable and will be properly handled by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [98/80]
end | ||
end | ||
|
||
def quote(value) | ||
# The built-in Sanitization of Rails does not handle time ranges very well. We work around this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [99/80]
lib/sneaky-save.rb
Outdated
send :arel_attributes_values | ||
attributes_with_values = send :attributes_with_values_for_create, attribute_names | ||
attributes_with_values.each_with_object({}) do |attribute_value, hash| | ||
hash[self.class.send(:arel_attribute, attribute_value[0])] = attribute_value[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [85/80]
lib/sneaky-save.rb
Outdated
send :arel_attributes_with_values_for_create, attribute_names | ||
else | ||
send :arel_attributes_values | ||
attributes_with_values = send :attributes_with_values_for_create, attribute_names |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics/LineLength: Line is too long. [85/80]
if result.respond_to?(:encoder) | ||
[definition.name.to_sym, quote(result.encoder.encode(value))] | ||
else | ||
raise RuntimeError.new('Unknown Type Casted Struct') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantException: Redundant RuntimeError.new call can be replaced with just the message.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Rails 6 compatibility
In rails 5 id_in_database is introduced and some validation checks are build upon this new attribute (e.g.: uniqueness).