On Saturday, August 2, 2014 2:12:31 AM UTC+1, Ruby-Forum.com User wrote:
>
>
> We would like to extract it, and the first thought was 'gem'. 
>
> And thus the questions arise :) 
>
> Assuming making a gem is the correct idea, I'm going to need to call 
> ActiveRecord methods such as 'create' from within the gem.  How do I 
> create or simulate a database that the gem can connect to?  When I do 
> get passed that question, how do I tell the gem to be able to 'know' 
> when it is just me testing it, and it being in a real rails environment? 
>
>
> Writing your code as a rails engine should address this. When loaded in 
your app, database settings etc are inherited (or rather if the models 
inherit from ActiveRecord::Base and your app has already configured 
activerecord then the engine's activerecord models will also use those 
settings)

When you're writing tests, depending on what you doing, you can either test 
it as you would any plain ruby code (you may have to configure an 
activerecord connection) and/or load your engine inside a dummy 
application. You can generate a skeleton with

    rails plugin new my_engine

Personally I only find this useful if the shared code starts having 
controllers, assets etc. If it's just a bunch of modules people can include 
or some useful baseclasses or some pure ruby code, then just a plain non 
engine gem is less hassle (bundler has a template for this). It's also not 
a big deal to start off as the latter and then turn it into a full blown 
engine later on.

Fred

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/c919adf7-8b62-4975-9c3b-1a9313947fd5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to