Non-rails application with RSpec, ActiveRecord and Factory Girl - ruby

How to properly implement RSpec, ActiveRecord and Factory Girl in non-rails application. I also need that every test case would run on a clean database.
Thanks!

ActiveRecord outside of Rails is a pretty common request, so there's plenty of blog posts and tutorials on getting it done, just Google it. Using it outside of Rails isn't very different from using it within Rails. The major difference is that you'll have to handle the things that Rails usually does for you, like creating the connection, and handling migrations.
RSpec was not designed specifically for Rails, so using that outside of Rails is a non-issue. Just follow the standard tutorial.
Same thing for factory_girl. Rails support was separated from factory_girl proper and moved into a separate gem, so using it without Rails is also simple. Its standard README should be sufficient.
For cleaning the database, use the database_cleaner gem.

Related

How to store Configatron persistently, after Ruby app has run

I'm pretty new to Ruby (though not to programming) and am trying to create a persistent config. Though I thought using Configatron would automatically make my config persistent, it does not seem to be the case. How would I make this persistent throughout multiple runs? Should I store this to a file? If so, how? I would think a ~/.myapp file might be good?
Persistence in Rails
If you're using Ruby on Rails, you need to modify your initializers for this gem:
app/config/configatron/defaults.rb
app/config/configatron/{development,test,production}.rb
Persistence Without Rails
Ruby Modules
If you're using Ruby itself, rather than the Rails framework, then you need to persist your configuration as a Ruby module (e.g. require "my_configuration") or serialize the Configatron class if it can serialized. It looks like the goal of the gem is to avoid serialization, so requiring a module that contains your configuration as executable code seems to be the canonical approach for this gem.
Serialization
I don't use this gem myself, so you'll have to experiment with serializers to see if they work for your use case. For serialization, I'd recommend using one of:
YAML::Store
PStore
Marshal
However, if you're using serialization to save and load your configuration, I'm not entirely sure why you'd need this gem in the first place. Your mileage may certainly vary.

Typical way of hosting a Ruby WebSocket service that uses gems?

While developing JavaScript apps, I usually create an API app, totally separate from the UI app. For the API, I usually use Sinatra.
I'm developing a JavaScript app that will use a WebSocket service I build. I'd like to use Ruby (em-websocket for now) and ActiveModel for data models. I want to keep this really lightweight, like a Sinatra app is for a RESTful API.
It seems my WebSocket service will simply be a ruby script invoked via "ruby web_socket_service.rb". I'd like to be able to use various gems (like activerecord, capistrano, and nokogiri) with this WebSocket service. What's the most typical way of accomplishing this?
Would I be better off creating a standalone gem to contain my models and the WebSocket service script and then host my WebSocket service from that? Or maybe simply include the gems and models directly in the script via "gem 'name'? Or, is there some special library or framework commonly-used to tackle this?
Look at a Rails app. That's the approach I would take if your WebSocket service starts to grow towards a medium-sized app. I.e. bin, lib, Rakefile, and a Gemfile for your gems and bundler.
For smaller apps you can still use a Gemfile and bundler to manage the included gems. This locks gem versions so you won't have conflicts if you deploy to other servers. And then just put everything into one or two script files, similar to Sinatra.
Creating standalone gems is really only useful for libraries or application parts that are reusable across many applications. This doesn't sound like that sort of thing.

Best practice for gem that creates its own database

I have a gem that I want to be capable of creating its own database (and later making migrations to that database if necessary). The gem uses ActiveRecord when reading and writing to the database. Short of embedding my gem in a non-serving Rails application just to get the necessary rake tasks, is there a best practice or community-promoted method for doing this? Attaching a whole Rails infrastructure to my little command-line only app just to get future migration upgrades seems like way too much overhead.
You can use DataMapper, which is a relatively lightweight ORM system (compared to ActiveRecord) in combination with an SQLite database. You don't need to use Rails for this, DataMapper drops nicely into a regular app, even something that isn't web-based.
You can use standalone_migrations gem to manage ActiveRecord environment outside of Rails: https://github.com/thuss/standalone-migrations. If you include your migrations or schema into a gem package, consumers of your gem can recreate db structure from scratch. I agree with robbrit that SQLite is the easiest choice for a database.

How difficult is it to switch from ActiveRecord to DataMapper in Rails 3.1?

I'm starting a new project and I have maybe three resources defined. Not a lot of code so far. (Rails 3.1)
But I'm interested in trying out DataMapper. I'm used to ActiveRecord (and actually enjoy it) but I'm always on the lookout for new things. Plus, my application uses Backbone.js but I don't believe that's relevant.
So how hard is it to switch out the ORM "mid-app" like this and do you think the learning curve to DM is that hard?
PS, there is a chance that I might be using other engines alongside my application. Such as MongoDB running along with Postgres. Will DM be at an advantage there?
To use Datamapper itself, there isn't much to it but it is some of the Rails niceties that require additional work (like the SQL execution times in "rails s") and there is also the rake tasks.
Check out dm-rails - They have a template that you use to provision the initial Rails project that sets it up with everything for Datamapper. You can also look through the source and see how it hooks everything. There is a small issue if you use database-backed session stores with Datamapper, which involves a monkey patch.

How to use activerecord alone?

I prepare to develop one project, which has no UI. The project just need to interact with database, so is there any example for reference ?
Does your app have ties to a Rails-based app and need to read the Rails configuration files? Or, is it entirely stand-alone and have no Rails interaction?
ActiveRecord is OK for that, but if I don't need Rails compatibility I use Sequel. It's a great ORM that I find to be much more flexible.
If you need Rails compatibility and want to use ActiveRecord, look into using rails runner. From the docs:
runner runs Ruby code in the context of Rails non-interactively. For instance:
$ rails runner "Model.long_running_method"
Rails runner is for command-line apps that don't need the HTTPd server or user-interface of Rails. I use them for things like an app that runs daily to ftp files from a site for analysis. It has to write to the database, so it has access to all the models I've defined, but it never needs to present anything to the user since it is invisible to them.

Resources