jordi
jordi

Reputation: 161

how to organize models used by backend and frontend in a rails application

I have a Rails application with a frontend for users and a configuration interface for the editing team. Both interfaces are really different but they use the same DB and the same models. Following the "everything slim but the fat controllers" philosophy models have accumulated methods used by both the backend and the frontend as well as independent methods.

For instance there are some constants in the Product model used by both but a self.find_by_week_logs(week_id) used only by the backend or a self.deliveries_per_page (pid) used only by the frontend.

How should I best organize this?

I so far thought or 3 possible solutions but I think they are not good enough.

class Product < Activerecod:Base
  common stuff
  # Frontend 
  Front end stuff
  # Backend 
  Back end stuff
end

class Product < Activerecod:Base

 common stuff
 module Frontend 
   module ClassMethods
     Frontend stuff
   end
   Frontend stuff
  end
  module Backend 
   module ClassMethods
     Backend stuff
   end
   Backend stuff
  end
  include Backend
  extend Bancked::ClassMethods

end

Any other solution/idea?

Upvotes: 0

Views: 445

Answers (1)

ShiningRay
ShiningRay

Reputation: 1008

create folders with the model name in app/models, here it's app/models/products

extract front end related codes to app/models/products/front_end.rb: you can use ActiveSupport::Concern to help:

module Product::FrontEnd
  extend ActiveSupport::Concern
  module InstanceMethods
    ...
  end
  module ClassMethods
    ...
  end
end

then in products.rb:

class Product < ActiveRecord::Base
  include FrontEnd
end

Upvotes: 1

Related Questions