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

Add rails 7 version specific fix for AttachmentSizeValidator #73

Merged
merged 1 commit into from
Dec 14, 2021

Conversation

yorzi
Copy link

@yorzi yorzi commented Dec 11, 2021

I compared the COMPARE_CHECKS (Rails 7) and CHECKS (Rails 6) - This can be replaced directly.

unless value.send(CHECKS[option], option_value)
operator = Rails::VERSION::MAJOR >= 7 ? COMPARE_CHECKS[option] : CHECKS[option]

unless value.send(operator, option_value)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/Next: Use next to skip iteration.


unless value.send(CHECKS[option], option_value)
operator = Rails::VERSION::MAJOR >= 7 ? COMPARE_CHECKS[option] : CHECKS[option]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/TrailingWhitespace: Trailing whitespace detected.

@@ -35,8 +35,9 @@ def validate_each(record, attr_name, value)
options.slice(*AVAILABLE_CHECKS).each do |option, option_value|
option_value = option_value.call(record) if option_value.is_a?(Proc)
option_value = extract_option_value(option, option_value)

unless value.send(CHECKS[option], option_value)
operator = Rails::VERSION::MAJOR >= 7 ? COMPARE_CHECKS[option] : CHECKS[option]
Copy link

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. [91/80]

@ssinghi ssinghi merged commit c9e6f9d into kreeti:master Dec 14, 2021
@scottsherwood
Copy link

@ssinghi I was just about to look into this and have seen that there is already a fix in place. Would it be possible to create a 7.0.2 tag that contains this fix at all please?

@atomical
Copy link

I'm using master and still getting an error using 7.0.3.1:

       uninitialized constant Paperclip::Validators::AttachmentSizeValidator::CHECKS
     
                   unless value.send(CHECKS[option], option_value)
                                     ^^^^^^

@atomical
Copy link

I see the problem. Some extensions require paperclip in the Gemfile. Is there a way to satisfy that dependency with kt-paperclip?

BrVer pushed a commit to BrVer/kt-paperclip that referenced this pull request Dec 3, 2022
@vnguyen13
Copy link

Is this pushed onto the master branch yet? I'm having the same issue and I'm running 6.4.2.

@atomical
Copy link

I put this in an initializer.

module Paperclip
  module Validators
    class AttachmentSizeValidator
      CHECKS = COMPARE_CHECKS
    end

    class AttachmentPresenceValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        if record.send("#{attribute}_file_name").blank?
          record.errors.add(attribute, :blank, **options)
        end
      end

      def self.helper_method_name
        :validates_attachment_presence
      end
    end
  end
end

@vnguyen13
Copy link

I put this in an initializer.

module Paperclip
  module Validators
    class AttachmentSizeValidator
      CHECKS = COMPARE_CHECKS
    end

    class AttachmentPresenceValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        if record.send("#{attribute}_file_name").blank?
          record.errors.add(attribute, :blank, **options)
        end
      end

      def self.helper_method_name
        :validates_attachment_presence
      end
    end
  end
end

You are the real MVP! Thank you. This fixed my issue with it.

@weeggs
Copy link

weeggs commented Jun 15, 2023

@atomical thank you
This solution also solved my issue.

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.

6 participants