Forum: Ruby on Rails How to approach the creation of a rails specific gem?

571b84c7de90293b010f39449b37c9e4?d=identicon&s=25 Carl Schwope (lowest0ne)
on 2014-08-02 03:11
I am creating an application using rails and spree at work.  One point
of value is the administrator being able to upload files that will seed
the database.


Our original solution was datashift_spree:
https://github.com/autotelik/datashift_spree

Two problems arose.  1) apparently, the datashift bundle is 250 MB and
was hindering our deployment to heroku.  2) We couldn't figure out how
to use it from within a deployed application anyway.

After a few people spent a few days with these problems, I ended up just
writing the functionality myself ( no datashift at all ).  We have two
applications being developed, and the code is valuable to both.

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?

Thanks.
24f49d448968ea24c8630c31fae12758?d=identicon&s=25 Bala Paranj (Guest)
on 2014-08-18 00:54
(Received via mailing list)
Since the dependency is on Rails framework, the right approach is to
create
a RailsEngine that you can share across your Rails applications not gem.

Bala Paranj
www.rubyplus.com
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2014-08-18 06:11
(Received via mailing list)
On Sun, Aug 17, 2014 at 5:53 PM, Bala Paranj <bparanj@gmail.com> wrote:

>> I am creating an application using rails and spree at work.  One point
>>
>> 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?
>>
>

There's no reason a rails engine cannot be a gem. It starts out life
that
way if you build it with rails anyway, since it creates a .gemspec file
for
the engine.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2014-08-18 10:49
(Received via mailing list)
On Saturday, August 2, 2014 2:12:31 AM UTC+1, Ruby-Forum.com User wrote:
> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.