mobility icon indicating copy to clipboard operation
mobility copied to clipboard

Feature: Generators read backend and options from model

Open thatguysimon opened this issue 6 years ago • 1 comments

Hi, First of all, thanks for the amazing work on this gem!

Context

While playing around with it, I've encountered the following issue - given I have a model that uses 2 separate mobility backends, the table backend and the key-value backend, if the default backend is set to key-value, an error is thrown when trying to generate the translations table.

class Post < ApplicationRecord
  extend Mobility

  translates :title, backend: :key_value, type: :string
  translates :body, backend: :table
  ...
Mobility.configure do |config|
  config.default_backend = :key_value
end

Running the generator:

rails generate mobility:translations post body:text

Expected Behavior

I expected a post_translations table to be created since I did configure at least one attribute to use the :table backend.

Actual Behavior

the following error:

The key_value backend does not have a translations generator.

Possible Fix

I easily worked around this by temporarily setting the default backend to :table, running the generator, then setting the default backend back to :key_value, but maybe this is worth fixing, or at least mentioning this case in the docs.

Thanks again! Simon.

thatguysimon avatar May 19 '18 16:05 thatguysimon

Thanks for the issue! You're right that Mobility should handle this "intelligently", by looking at the model and checking the appropriate attribute backend, but actually the generators are kind of dumb as currently designed. They only look at your setting for Mobility.default_backend and nothing else.

The way to get around this in your situation (without changing the code) is to specify the backend explicitly with:

rails generate mobility:translations post body:text --backend=table

But there are other reasons why the generators should check the model, e.g. if you specify a different table name currently the generator will not see this and use post_translations regardless, which I consider a bug.

So, I'll leave this open and treat it as a feature request rather than a bug.

shioyama avatar May 19 '18 22:05 shioyama

Closing, no time to support feature requests.

shioyama avatar Mar 20 '24 08:03 shioyama