0

In a model of mine I'm using the bing translator gem for automatic translation of a model attribute via a after_create callback:

class Place < ActiveRecord::Base

  after_create :auto_translate

  ....

  # AUTO_TRANSLATE STUFF
  def initialize_bing_translator(bing_id, bing_secret)
    t = BingTranslator.new(bing_id, bing_secret)
    <do other stuff>
  end

  def auto_translate
    <do stuff>
  end

  <further auto_translate methods>

The whole bunch of functions seems to bloat the model code a little so I want to put it into some extra module. Where exactly shall I place the .rb-file? Is this a use-case for a concern (a concept I did not fully understood)? Is it better to define a seperate module in the model file itself or to place it in /lib/user_modules/? Is there sth like a rule-of-thumb? The information available on the web are confusing me a little and I'd be glad if someone could shed some light on that issue for me!

A. Neumann
  • 488
  • 2
  • 15
  • Will [this](http://stackoverflow.com/questions/2328984/rails-extending-activerecordbase) question help? – Pavel Bulanov Jun 01 '16 at 10:54
  • for me it can be resided in two places: 1. `lib/models/...` and 2. `app/models/concern`. the modules from both places should be explicitly included/extended to the model – Малъ Скрылевъ Jun 01 '16 at 10:58
  • Can you explain why you do it this way? The proposal down below for example is somehow different (put model related stuff in a sub-folder) – A. Neumann Jun 01 '16 at 11:14

2 Answers2

1

I started putting stuff like that in subfolders of models and namespacing things accordingly. In your specific case I'd put it in something like models/place/translator.rb (or similar) and call the class Place::Translator.

If you just want a sort of logical separation but will not use the same functionality elsewhere you can use concerning instead of a separate concern.

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
1

There are many options and most of them are primarily opinion-based. It depends on if you plan to reuse that class, how complex your application already is and if the new class has any external dependency.

I would think about: create your own gem, add to app/models, add a new app/translators directory, perhaps in lib...

spickermann
  • 100,941
  • 9
  • 101
  • 131