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

Attachments are optional, but Rails 5.1 doesn't know that #246

Closed
walterdavis opened this issue Jun 12, 2017 · 7 comments
Closed

Attachments are optional, but Rails 5.1 doesn't know that #246

walterdavis opened this issue Jun 12, 2017 · 7 comments

Comments

@walterdavis
Copy link

walterdavis commented Jun 12, 2017

I tried using the latest release of fae on Rails 5.1 (Ruby 2.3.1). I was able to work around the copied generators error, but then could not create a new Article (following the example in the quick start). I traced this to the Hero Image and PDF being (implicitly) required at the model layer, but not in the UI. Overriding the Fae::File with

belongs_to :fileable, polymorphic: true, touch: true, optional: true

...and similar on the Fae::Image allowed the interface to work.

There was an additional oddity with the UX in the form, related (I suspect) to the system marking these form elements as not required. After submitting the form, I would briefly see an error message at the top saying that I should "check the form fields for errors". This would disappear after a very short time, but no fields were marked as having errors. When I added = debug @item.errors.full_messages to the relevant _form partial, I was able to see that the image and file were being required, as noted above.

@jamesmk
Copy link
Member

jamesmk commented Jun 14, 2017

Thanks for the report @walterdavis. We are currently working on Rails 5.1 support, but with it's recent deprecations are finding it difficult to support both Rails 4 and 5.1. If you want to work with a stable version of Fae I'd recommend using Rails 5.0 for now.

@Spone
Copy link
Contributor

Spone commented Jul 14, 2017

Hi, I also need attachments to work with Rails 5.1.

EDIT: see below for a better way

I tried to override Fae::File and Fae::Image by putting this in an initializer:

# Attachments are optional, but Rails 5.1 doesn't know that
# https://github.com/wearefine/fae/issues/246
require 'file_size_validator'

module Fae
  class File < ActiveRecord::Base

    include Fae::BaseModelConcern
    include Fae::FileConcern
    include Fae::AssetsValidatable

    mount_uploader :asset, Fae::FileUploader

    validates :asset,
      file_size: {
        maximum: Fae.max_file_upload_size.megabytes.to_i
      }

    belongs_to :fileable, polymorphic: true, touch: true, optional: true

    def readonly?
      false
    end

  end
end

module Fae
  class Image < ActiveRecord::Base

    include Fae::BaseModelConcern
    include Fae::ImageConcern
    include Fae::AssetsValidatable

    attr_accessor :redirect
    mount_uploader :asset, Fae::ImageUploader

    after_save :recreate_versions

    validates :asset,
      file_size: {
        maximum: Fae.max_image_upload_size.megabytes.to_i
      }

    belongs_to :imageable, polymorphic: true, touch: true, optional: true

    def readonly?
      false
    end

    private

    def recreate_versions
      asset.recreate_versions! if Fae.recreate_versions && asset.present?
    end

  end
end

It seems to fix the issue, but breaks the scaffold generator:

$ rails g fae:scaffold MyModel name description:text active:boolean
Running via Spring preloader in process 32156
[WARNING] Could not load generator "generators/fae/scaffold_generator". Error: undefined method `expand_path' for Fae::File (call 'Fae::File.connection' to establish a connection):Class.
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.1.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/fae-rails-1.5.1/lib/generators/fae/base_generator.rb:3:in `<class:BaseGenerator>'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/fae-rails-1.5.1/lib/generators/fae/base_generator.rb:2:in `<module:Fae>'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/fae-rails-1.5.1/lib/generators/fae/base_generator.rb:1:in `<top (required)>'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/fae-rails-1.5.1/lib/generators/fae/scaffold_generator.rb:1:in `require_relative'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/fae-rails-1.5.1/lib/generators/fae/scaffold_generator.rb:1:in `<top (required)>'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `block in require'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:258:in `load_dependency'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/command/behavior.rb:82:in `block (2 levels) in lookup'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/command/behavior.rb:78:in `each'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/command/behavior.rb:78:in `block in lookup'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/command/behavior.rb:77:in `each'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/command/behavior.rb:77:in `lookup'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/generators.rb:250:in `find_by_namespace'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/generators.rb:267:in `invoke'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/commands/generate/generate_command.rb:24:in `perform'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/command/base.rb:63:in `perform'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/command.rb:44:in `invoke'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.2/lib/rails/commands.rb:16:in `<top (required)>'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `block in require'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:258:in `load_dependency'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
/home/hans/projects/myproject/bin/rails:9:in `<top (required)>'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:286:in `load'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:286:in `block in load'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:258:in `load_dependency'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:286:in `load'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/usr/local/opt/rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
-e:1:in `<main>'
Expected boolean default value for '--markerb'; got :erb (string)
Could not find generator 'fae:scaffold'. Maybe you meant 'css:scaffold', 'erb:scaffold' or 'scaffold'
Run `rails generate --help` for more options.

Can you help? @jamesmk let me know if there anything I can do to help with Rails 5.1 support.

@Spone
Copy link
Contributor

Spone commented Aug 11, 2017

I need to get Fae to work with Rails 5.1 (especially attachments) in the coming weeks.

@jamesmk please let me know how I can help, I can spend some time on this.

@jamesmk
Copy link
Member

jamesmk commented Aug 12, 2017

@Spone Rails 5.1 support will be rolled into Fae 2.0 due to the difficulty of supporting Rails 4 and 5.1 in the same engine. 2.0 will be on a longer timeline as there's lots we want to improve. We'd appreciate any help with the Rails 5.1 support and will accept PRs into the v2.0 branch. I wouldn't recommend linking a production app to the branch though as it's in active development and 2.0 means we'll be making breaking changes.

I think you're on the right path with your solution above. The error you're getting was likely fixed by your recently merge PR: #282

thanks

@Spone
Copy link
Contributor

Spone commented Aug 12, 2017

@jamesmk Thanks for getting back to me. The solution described below fixes the issue.

Regarding Fae 2.0, do you mind if I add feature suggestions as issues, so we can discuss them and maybe include them in v2.0? Then I may be able to spend time on some of them.

@jamesmk
Copy link
Member

jamesmk commented Aug 14, 2017

@Spone Feature requests are welcome!

@Spone
Copy link
Contributor

Spone commented Aug 16, 2017

Quick follow-up on overriding the classes: it's easier to put them in their own files and take advantage of built-in Rails overriding logic (instead of adding an initializer). So I'd advise creating these files in your app:

app/models/fae/file.rb

require 'file_size_validator'

module Fae
  class File < ActiveRecord::Base

    include Fae::BaseModelConcern
    include Fae::FileConcern
    include Fae::AssetsValidatable

    mount_uploader :asset, Fae::FileUploader

    validates :asset,
      file_size: {
        maximum: Fae.max_file_upload_size.megabytes.to_i
      }

    belongs_to :fileable, polymorphic: true, touch: true, optional: true

    def readonly?
      false
    end

  end
end

app/models/fae/image.rb

require 'file_size_validator'

module Fae
  class Image < ActiveRecord::Base

    include Fae::BaseModelConcern
    include Fae::ImageConcern
    include Fae::AssetsValidatable

    attr_accessor :redirect
    mount_uploader :asset, Fae::ImageUploader

    after_save :recreate_versions

    validates :asset,
      file_size: {
        maximum: Fae.max_image_upload_size.megabytes.to_i
      }

    belongs_to :imageable, polymorphic: true, touch: true, optional: true

    def readonly?
      false
    end

    private

    def recreate_versions
      asset.recreate_versions! if Fae.recreate_versions && asset.present?
    end

  end
end

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

No branches or pull requests

3 participants