Skip to content

The attachment files options may be serialized in the wrong way #136

Description

@akitaonrails

I noticed that when the attachinary_file_field_options fetches existing attachinary_files associated with my model it serializes the files: [<Attachinary::File ...>] like files: [{ file: { id: 123 ...} }, {file: { id: 456 ...} } ] instead of the correct files: [ {id: 123 ...}, {id: 456, ...} ].

And this makes it go wrong is many ways (remove button doesn't work, posting this option and assigning it to the attachment association makes it delete all associations and recreate them all again, etc.

I was finally able to figure out a fix like this:

require "attachinary/orm/active_record"

module Attachinary
  module FileMixin
    def as_json(options = {})
      super(only: [:id, :public_id, :format, :version, :resource_type], methods: [:path])
    end
  end

  module ViewHelpersExtension
    def attachinary_file_field_options(model, relation, options={})
      options = super(model, relation, options)
      options[:html][:data][:attachinary][:files].map!(&:attributes)
      options
    end
  end

  module ViewHelpers
    prepend ViewHelpersExtension
  end
end

And I add it to the config/initializers/attachinary.rb file. Sounds like it is working properly but I am not sure if this is a bug or if it is something I am doing wrong. All the rest of the configuration is default to what the README instructs.

Activity

  1. changed the title [-]The JS side files options may be serialized in the wrong way[/-] [+]The attachment files options may be serialized in the wrong way[/+] on Sep 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions