Giter VIP home page Giter VIP logo

Comments (6)

davetron5000 avatar davetron5000 commented on June 6, 2024

This is weird. Is your class an ActiveRecord?

This is how the method is implemented:

https://github.com/stitchfix/stitches/blob/master/lib/stitches/errors.rb#L71

what it's doing is basically iterating over all the active record errors on your object and, if the field with an error itself is an active record, get those errors.

it could be that the way it does this isn't quite right. The code:

message = if object.send(field).respond_to?(:errors)

is assuming that if the field's value responds to errors, it's therefore an ActiveRecord and thus errors will return an active record errors object, not a hash.

It sounds like in your case, errors on either your object, or an object your object references has an errors method that returns a hash and not an active record errors object.

from stitches.

sqlninja avatar sqlninja commented on June 6, 2024

The class is inheriting from ApplicationRecord

class Medium < ApplicationRecord
  ...

  validate :ojbect_size_valid

 ...

  def ojbect_size_valid
    errors.add(:object, 'should be less than or equal to 5MB for images') if object_file_size > 5.megabytes && image_type?
    errors.add(:object, 'should be less than or equal to 15MB for gifs and videos') if object_file_size > 15.megabytes && (gif_type? || video_type?)
  end

  private

  def image_type?
    object_content_type =~ /jpeg/ || object_content_type =~ /jpg/ || object_content_type =~ /png/
  end

  def gif_type?
    object_content_type =~ /gif/
  end

  def video_type?
    object_content_type =~ /video/
  end

  def pdf_type?
    object_content_type =~ /pdf/
  end
end

The AppilcationRecord is just inheriting from ActiveRecord

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

from stitches.

davetron5000 avatar davetron5000 commented on June 6, 2024

Hm. My guess is that the code is detecting that .object returns a value that responds to errors but that errors is actually a hash. What is the type of object on this class?

from stitches.

sqlninja avatar sqlninja commented on June 6, 2024

Whoops meant to leave that in:
has_attached_file :object, styles: ->(f) { f.instance.check_file_type }

object is a paperclip attachment to the medium

from stitches.

davetron5000 avatar davetron5000 commented on June 6, 2024

https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/attachment.rb#L83

Stitches assumes if the object responds to #errors it's an active record. This is probably a bug in stitches.

To solve your immediate problem, you could inline the implementation of from_active_record and fix it so it doesn't break. I probably can't look into a fix today, but maybe could this week. I think it's as simple as checking if errors is an ActiveModel::Errors

from stitches.

sqlninja avatar sqlninja commented on June 6, 2024

Believe I figured it out...
So the "field" isn't really object, but instead object_file_size
Paperclip basically yields a wrapper to the various metadata about the object

from stitches.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.