Skip to content
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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

g0ody
Copy link

@g0ody g0ody commented Jul 23, 2018

In rails 5 id_in_database is introduced and some validation checks are build upon this new attribute (e.g.: uniqueness).

copy_internal(data, self, "@new_record")
copy_internal(data, self, "@destroyed")

!!self.id

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.

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 *"

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]

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

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 }.

!!(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(', ')

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.


@new_record = false
!!(self.id ||= new_id)
sneaky_attributes_without_id = self.sneaky_attributes_values.except { |key| key.name == 'id' }

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.

if id.nil? && !prefetch_pk_allowed
attributes_values.reject! { |key, _| key.name == 'id' }
end
sql = <<~SQL

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.

attributes_values = sneaky_attributes_values
mapping = sneaky_attributes_without_id
.map { |key, value| [key.name.to_sym, value]}
.to_h

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.


attributes_values = sneaky_attributes_values
mapping = sneaky_attributes_without_id
.map { |key, value| [key.name.to_sym, value]}

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 }.

column_keys = sneaky_attributes_without_id.keys.map(&:name)
dynamic_keys = sneaky_attributes_without_id.keys
.map { |key| ":#{key.name}" }
.join(', ')

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.

end
column_keys = sneaky_attributes_without_id.keys.map(&:name)
dynamic_keys = sneaky_attributes_without_id.keys
.map { |key| ":#{key.name}" }

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.

@@ -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' }

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.

@g0ody g0ody force-pushed the master branch 3 times, most recently from 3b08060 to 08d34fd Compare July 23, 2018 14:46
if definition.able_to_type_cast?
[definition.name.to_sym, definition.type_cast_for_database(value)]
else
[definition.name.to_sym, value]

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.


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)]

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" }

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.

if result.respond_to?(:encoder)
[definition.name.to_sym, result.encoder.encode(value)]
else
raise RuntimeError.new('Unknown Type Casted Struct')

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.

.join(", ")

constraint = if avoid_insert_conflict.present?
"ON CONFLICT (#{[avoid_insert_conflict].flatten.join(', ')}) "\

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(", ")

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}" }

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" }

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

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(", ")

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}" }

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(", ")

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.

.except { |key| key.name == "id" }

column_keys = sneaky_attributes_without_id.keys
.map { |key| "\"#{key}\"" } # to avoid conflicts with column names

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(", ")

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}" }

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

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]

target.instance_variable_set(key, source.instance_variable_get(key))
end

def generate_constraint(avoid_insert_conflict, column_keys, column_keys)

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

@einzige
Copy link
Owner

einzige commented Aug 24, 2018

There is another problem with arel_attributes_with_values_for_create

# 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)

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

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

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]

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]

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]

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

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')

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants