thoughtbot/paperclip

View on GitHub
lib/paperclip/validators/attachment_content_type_validator.rb

Summary

Maintainability
B
4 hrs
Test Coverage

Assignment Branch Condition size for validate_each is too high. [18.92/15]
Open

      def validate_each(record, attribute, value)
        base_attribute = attribute.to_sym
        attribute = "#{attribute}_content_type".to_sym
        value = record.send :read_attribute_for_validation, attribute

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Similar blocks of code found in 2 locations. Consider refactoring.
Open

  module Validators
    class AttachmentContentTypeValidator < ActiveModel::EachValidator
      def initialize(options)
        options[:allow_nil] = true unless options.has_key?(:allow_nil)
        super
lib/paperclip/validators/attachment_file_name_validator.rb on lines 2..75

Duplicated Code

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Tuning

This issue has a mass of 166.

We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

Refactorings

Further Reading

Line is too long. [96/80]
Open

        return if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])

Line is too long. [82/80]
Open

        if allowed_types.present? && allowed_types.none? { |type| type === value }

Argument value was shadowed by a local variable before it was used.
Open

        value = record.send :read_attribute_for_validation, attribute

This cop checks for shadowed arguments.

Example:

# bad

do_something do |foo|
  foo = 42
  puts foo
end

def do_something(foo)
  foo = 42
  puts foo
end

Example:

# good

do_something do |foo|
  foo = foo + 42
  puts foo
end

def do_something(foo)
  foo = foo + 42
  puts foo
end

def do_something(foo)
  puts foo
end

Line is too long. [85/80]
Open

        if forbidden_types.present? && forbidden_types.any? { |type| type === value }

Line is too long. [81/80]
Open

      # * +unless+: Same as +if+ but validates if lambda or method returns false.

Use Hash#key? instead of Hash#has_key?.
Open

        options[:allow_nil] = true unless options.has_key?(:allow_nil)

This cop (by default) checks for uses of methods Hash#haskey? and Hash#hasvalue? where it enforces Hash#key? and Hash#value? It is configurable to enforce the inverse, using verbose method names also.

Example: EnforcedStyle: short (default)

# bad Hash#haskey? Hash#hasvalue?

# good Hash#key? Hash#value?

Example: EnforcedStyle: verbose

# bad Hash#key? Hash#value?

# good Hash#haskey? Hash#hasvalue?

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Open

        record.errors.add attribute, :invalid, options.merge(:types => types.join(', '))

Checks if uses of quotes match the configured preference.

Example: EnforcedStyle: single_quotes (default)

# bad
"No special symbols"
"No string interpolation"
"Just text"

# good
'No special symbols'
'No string interpolation'
'Just text'
"Wait! What's #{this}!"

Example: EnforcedStyle: double_quotes

# bad
'Just some text'
'No special chars or interpolation'

# good
"Just some text"
"No special chars or interpolation"
"Every string in #{project} uses double_quotes"

Line is too long. [95/80]
Open

          raise ArgumentError, "You must pass in either :content_type or :not to the validator"

Line is too long. [88/80]
Open

        record.errors.add attribute, :invalid, options.merge(:types => types.join(', '))

Use the new Ruby 1.9 hash syntax.
Open

        record.errors.add attribute, :invalid, options.merge(:types => types.join(', '))

This cop checks hash literal syntax.

It can enforce either the use of the class hash rocket syntax or the use of the newer Ruby 1.9 syntax (when applicable).

A separate offense is registered for each problematic pair.

The supported styles are:

  • ruby19 - forces use of the 1.9 syntax (e.g. {a: 1}) when hashes have all symbols for keys
  • hash_rockets - forces use of hash rockets for all hashes
  • nomixedkeys - simply checks for hashes with mixed syntaxes
  • ruby19nomixed_keys - forces use of ruby 1.9 syntax and forbids mixed syntax hashes

Example: EnforcedStyle: ruby19 (default)

# bad
{:a => 2}
{b: 1, :c => 2}

# good
{a: 2, b: 1}
{:c => 2, 'd' => 2} # acceptable since 'd' isn't a symbol
{d: 1, 'e' => 2} # technically not forbidden

Example: EnforcedStyle: hash_rockets

# bad
{a: 1, b: 2}
{c: 1, 'd' => 5}

# good
{:a => 1, :b => 2}

Example: EnforcedStyle: nomixedkeys

# bad
{:a => 1, b: 2}
{c: 1, 'd' => 2}

# good
{:a => 1, :b => 2}
{c: 1, d: 2}

Example: EnforcedStyle: ruby19nomixed_keys

# bad
{:a => 1, :b => 2}
{c: 2, 'd' => 3} # should just use hash rockets

# good
{a: 1, b: 2}
{:c => 3, 'd' => 4}

Line is too long. [81/80]
Open

      # * +message+: The message to display when the uploaded file has an invalid

Use Hash#key? instead of Hash#has_key?.
Open

        unless options.has_key?(:content_type) || options.has_key?(:not)

This cop (by default) checks for uses of methods Hash#haskey? and Hash#hasvalue? where it enforces Hash#key? and Hash#value? It is configurable to enforce the inverse, using verbose method names also.

Example: EnforcedStyle: short (default)

# bad Hash#haskey? Hash#hasvalue?

# good Hash#key? Hash#value?

Example: EnforcedStyle: verbose

# bad Hash#key? Hash#value?

# good Hash#haskey? Hash#hasvalue?

Use Hash#key? instead of Hash#has_key?.
Open

        unless options.has_key?(:content_type) || options.has_key?(:not)

This cop (by default) checks for uses of methods Hash#haskey? and Hash#hasvalue? where it enforces Hash#key? and Hash#value? It is configurable to enforce the inverse, using verbose method names also.

Example: EnforcedStyle: short (default)

# bad Hash#haskey? Hash#hasvalue?

# good Hash#key? Hash#value?

Example: EnforcedStyle: verbose

# bad Hash#key? Hash#value?

# good Hash#haskey? Hash#hasvalue?

Line is too long. [82/80]
Open

      # You'll still need to have a virtual attribute (created by +attr_accessor+)

There are no issues that match your filters.

Category
Status