scarfacedeb / rails_admin_globalize_field

Tabbed interface and custom field type for globalize translations for Rails_admin
MIT License
51 stars 61 forks source link

Add support for ActiveStorage #38

Closed travismmorgan closed 5 years ago

travismmorgan commented 5 years ago

It would be great if there was support for ActiveStorage. Attachment for each language. Or maybe even just allow putting custom fields in a specified tab e.g. configure :en do field :attachment end

scarfacedeb commented 5 years ago

@travismmorgan It's already possible.

You can treat nested YourModel::Translation model as any other model and use has_one_attached with it too.

I tested it with the following config:

class Page < ApplicationRecord
    translates :title, :content
    accepts_nested_attributes_for :translations, allow_destroy: true

    Translation.class_eval do
      has_one_attached :image

      # To fix a bug when it won't save an attachment when no other attribute has been modified in the record
      def image=(*)
        self.updated_at = Time.zone.now unless changed?
        super
      end
    end
end

  config.model 'Page::Translation' do
    visible false
    configure :locale, :hidden do
      help ''
    end
    include_fields :locale, :title, :content, :image
  end

I have to override Page::Translation#image= method to force activerecord to save uploaded images when no other attributes have been modified. I'm not sure that it's the right thing to do in this situation, but it works. In any other cases, the code should work as expected.

travismmorgan commented 5 years ago

@scarfacedeb This is the error I am receiving when following your guide PG::NotNullViolation: ERROR: null value in column "record_id" violates not-null constraint DETAIL: Failing row contains (a5d5f29c-0dbd-462e-a172-18e7f6559277, subtitles, null, Lesson::Translation, 368fc912-0d98-4fba-86b6-3e7a7914f9fd, 2019-10-31 18:10:56.986265).

travismmorgan commented 5 years ago

class Lesson < ApplicationRecord belongs_to :course, optional: false has_and_belongs_to_many :regions

has_one_attached :english_subtitles
has_one_attached :korean_subtitles
has_one_attached :thumbnail

enum video_type: [ :mono_360, :stereo_360 ], _suffix: true

translates :name, :description
accepts_nested_attributes_for :translations, allow_destroy: true

Translation.class_eval do
    has_one_attached :subtitles

    # To fix a bug when it won't save an attachment when no other attribute has been modified in the record
    def subtitles=(*)
      self.updated_at = Time.zone.now unless changed?
      super
    end
end

end

scarfacedeb commented 5 years ago

I don't know why it's failing in your case. Unfortunately I have little experience working with active storage. You could try my test app to compare it against your code: https://github.com/scarfacedeb/rails_admin_test_ground

travismmorgan commented 5 years ago

Ok I found the issue but don't have a solution. It is saying null record_id because I am using uuid instead of int for my primary keys but the translations table uses int.

travismmorgan commented 5 years ago

@scarfacedeb do you know of a way to force the translation tables to be uuid instead of integer?

scarfacedeb commented 5 years ago

You’ll probably have to modify Translation class.

travismmorgan commented 5 years ago

I decided to just switch my primary keys back to integer and add a uuid column. All working now. Last question, I am using serializer to return the query data in json, how can I bypass the i18n and return all translations for a column?

scarfacedeb commented 5 years ago

See: https://github.com/globalize/globalize/blob/master/lib/globalize/active_record/instance_methods.rb#L148