Overwrite ActiveRecord's save method to use custom interface - ruby-on-rails

I got the following situation:
I have a rails application and two databases, one is for the Rails Application and the second database (running on the same DB Server Instance) is for a different non-web application.
My Rails Web Application may read from the (second) Database directly via SQL but it can't write to it because of security restrictions (the data has to be validated by the non-web application). So we wrote a little CLI interface based on SOAP for writing into the database.
My question is: Can I extend the ActiveRecord Model (Rails 3) in a way so that the reading goes as normal over the SQL connection but update/create/delete goes over our selfmade interface.

I think I found a good solution :)
# rtacconi: Thanks for your links but since Rails 3 you don't have to use these extensions because ActiveModel works table-less out of the box :)
I need full ActiveRecord support for reading the table but writing is done over my SOAP interface. This is because I can't validate the data in my Rails application.
So my solution is to overload the ActiveRecord::Persistence Module (can be found in activerecord-3.0.0/lib/active_record/persistence.rb.
This module is responsible for any write tasks to the DB connection. So instead of writing to the DB, my Persistence Layer calls the SOAP interface.
Best regards
Simon

You could implement a tableless model: ActiveRecord::Base Without Table or http://github.com/AnthonyCaliendo/acts_without_database
Than you can set/get data into an object using the SOAP library.

Related

How to pump data into Rails Models from external sources (not web forms)?

Are there any good practice on how can we insert data into a Rails App's models from an external source (not the web app), while still leveraging the ORM capabilities of active record? How possible is it for the ActiveRecord class be imported into our own external ruby script without the complete rails package? I was hoping to avoid writing raw SQLs as much as possible. Thanks!
Sure you can.
One way would be to use rake tasks within your app, which can access your ActiveRecord models. It will still load your app up, but not as a web service.
http://jasonseifer.com/2010/04/06/rake-tutorial
Also recommend this for more bulk style insert/updates:
https://github.com/zdennis/activerecord-import

How to setup Ruby on Rails app without an ORM, using only low-level database driver?

I want to create a Rails application using ArangoDB. It has Rails adapter, Guacamole, but it doesn't have some features I'd like to have (namely, AQL). So I'm thinking of building my own models using low-level Ashikawa driver.
What code do I need to write for this to function properly? I imagine I'll have to write an initializer to establish the connection, is there anything else that I'm missing?
Thank you.
Generating a app with rails new my_app -O will give you an app without ActiveRecord. The Mongoid Installation Guide has a step by step of how to get rid of ActiveRecord in an existing app.
I imagine that you would then establish a connection in an initialiser, and create either a model class that your models extend (in the style of ActiveRecord) or a mixin which your models include (ala Mongoid::Document). This module or class will house any shared functionality required.
You might want to extend ActiveModel::Model as it will give you inflection, validations and other features.

Exchange data between Rails Engines

I'm developing a Rails application which uses different Rails Engines to encapsulate functionality.
I want to exchange data between these Engines.
Is there a possibility to create some kind of public interface offered by the Engine to exchange data which I can use in the other engines / the Host Application? Or should I use some kind of JSON API for the access?
Here's a little example which should show what I mean:
Let's assume I have a "full" Rails application. I also have a user-Engine and a tweet-Engine mounted in this application. Now I want to access data from the user in the tweet-Engine.
Btw. Is there furthermore a way to create associations between Engine-Models?
I'm very glad if somebody can help me
Thanks in advance!
There are a few approaches to this. You can have a shared database where engines have models which access the same database to communicate between each other. See the rails guide for more information http://guides.rubyonrails.org/engines.html#using-a-class-provided-by-the-application
The other approach as you said is to build an API, although that it more applicable to separate applications.
in user engine application write controller action which will return xml or json and in tweet engine application just use net:http to get the data by calling the methods of user engine app.
Thank you

Rails: Active Model explanation?

What's the difference from Active Resource and Active Record in Rails?
ActiveRecord: ORM to manage data in a relational database through SQL. read more..
ActiveResource: Manage data through a REST API on another server, no longer a part of Rails, but extracted as a gem. read more..
ActiveModel: Behavior (mixins) to make any Ruby object act like it's ActiveRecord or ActiveResource by adding validations, callbacks, naming conventions, tracking, etc. read more..
ActiveRecord acts as a layer between a specific database and your application. It is a conventional Object Relationship Mapper (ORM).
ActiveResource acts as a layer between a more abstract network resource such as a REST server and your application. It acts as a sort of wrapper to make a REST resource appear as a database model so similar techniques can be used.

Best practice of implementing a webservice client in rails application

In situation, when I need a webservice client, which will be used by some action controllers, how should it be implemented? As it will have some constant values (addres, parameters names) and session key, refreshed every 30 minutes, I guess model stored in database isn't the best solution. What is the best practice?
Built the web service consumer as a library and place it in your /lib directory.
I would also recommend HTTParty for very simple consumption of web services. You could easily build a library to handle the task, a simple class with a few methods and toss it in your /lib directory and be on your way.
Good luck!
Rails 1.x used to have "actionwebservices" builtin but it has been removed from Rails 2.x. The last time I did this was to build an SSO server implemented as classes using XML-RPC. The code is not public unfortunately (done internally for my employer) but was under 1k LOC incl. comments... Plain Ruby.
Now, I'd probably use a lightweight framework like Sinatra or an equivalent.

Resources