Rails STI routing - ruby-on-rails-3

I've got an article model which uses STI. The sub classes are emotions, categories, gateways etc...
In my routes I've got
resources :emotions, :controller => 'articles'
resources :categories, :controller => 'articles'
resources :gateways, :controller => 'articles'
This makes all the different sub classes available at /articles/108 or emotions/108 or categories/108 - it doesn't matter which subclass you stick at the front they all work for all articles.
I would like all my url helpers to produce links to articles/id - at the moment they still go to the particular sub class.
How would I go about doing this?

If article is the base class of another derived class like gateway (class Gateway < Article), then maybe just use the urls generated by resources :articles. It should be possible to use the articles helper article_path(gateway) since the subclasses are derived from the base class.

Related

How can I set Ember embedded association keys to match expected nested_attributes_for format on the Rails side?

I've got an Ember model with a nested association. When I POST to the Rails server, Ember is sending:
Parameters: {"parent"=>{"name"=>"Jim", "kid"=>{"name"=>'Sara'}}
On the Rails side, the parent accepts_nested_attributes_for kid, which means that the Rails model is expecting:
Parameters: {"parent"=>{"name"=>"Jim", "kid_attributes"=>{"name"=>'Sara'}}
I'm currently handling this by editing the params hash in the Rails controller before I call create on the parent model. That's a hack, clearly. I'm sure there's some way to reconfigure the expected key for embedded associations. (I'm thinking it'd be preferable to do this on the client-side, but I don't have strong feelings about it.)
Any advice for how to handle this? Either on the Rails side or the Ember side is fine. Thanks.

Zend Framework 2, Entity Manager, and Doctrine 2

Had a question about what best practice might be for the implementation of "convenience" queries. In reference to this article:
http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/#toc-install-doctrine-modules
It's clear that the entity manager is available in the IndexController - he does a findAll to list the entire contents of the database. What if, however, we added a "band" column to the database, mapped it out, and wanted to query all albums by the Beatles? What if the Beatles albums were used rather often throughout the codebase (weak example, but you get it).
The EM only seems to be available in Controllers, and Classes don't really seem to be aware of the service locator.
Would you simply break out DQL right in the controller, and repeat the DQL in every controller that needs it? (not very DRY)
Do we instead finagle some access to the EM from the Entity, or Model?
Doesn't seem as cut-and-dry as straight Zend_Db usage where you can fire queries anywhere you like, cheating to get things done.
Thanks for helping me cross over into a "real" ORM from the Table Gateway world.
Erm, Doctrine 2 is able to handle Relationships (e.g.: Bands to Albums and vice-versa)
The EntityManager can be made available in every single class you wish, as long as you define the class as a service. I.e. inside your Module.php you can define a factory like this:
// Implement \Zend\ModuleManager\Feature\ServiceProviderInterface
public function getServiceConfig() {
return array(
//default stuff
'factories' array(
'my-album-service' = function($sm) {
$service = new \My\Service\Album();
$service->setEntityManager($sm->get('doctrine.entitymanager.orm_default'));
return $service;
}
)
)
);
You can then call this class from every Class that is aware of the ServiceManager like $this->getServiceLocator()->get('my-album-service')
This class would then automatically be injected with the Doctrine EntityManager.
To be clear: All queries you'd do SHOULD be located inside your Services. You'd have your Entities, which are basically the DB_Mapper from Doctrine 2, then you have your Services, which run actions like add(), edit(), findAll(), findCustomQuery(), etc...
You would then populate your Services with Data from the Controllers, the Service would give data back to the controller and the controller would pass said data to the view. Does that make sense to u and answer your question?

rails mailer default :to

I found all my UserMailer functions send mail to user.email, and I was looking out for a way to set it as default, anybody tried out or thought of similar hack before?
Generally what I have is instead of all my mailer classes inheriting ActionMailer::Base, I inherit them with my common mailer class which inherits ActionMailer
There in you can specify the default :to which will be used for all you mailer classes..

cancan: getting past undefined method `find' for Userhome:Class

cancan did not work with a controller that did not have a class. So I created the userhome.rb model:
class Userhome
end
There is an action in the userhome controller that accesses a page in another directory/class. An attempt to access it yields the following error:
undefined method `find' for Userhome:Class
Is the best thing for me to do...:
delete the userhome model, and
remove "load_and_authorize_resource" from the userhome controller, and
just lock the application down with cancan in every other area possible?
Or is there a workaround to deal with this error?
Take a look at the CanCan documentation on non-RESTful controllers.
A "resource" is the "thing" that your controller is responsible for listing, creating, updating, etc. It often is a model, but need not be (e.g. you might have a "search results" resource that doesn't have a corresponding model).
If your controller really isn't dealing with a resource, then you may want to just use authorize! as appropriate within the controller, but if the controller is dealing with a resource but there is no corresponding model (which sounds like it may describe your situation) then you may want to use authorize_resource and specify that there is no corresponding class. This lets you "pretend" that you have a resource (i.e. you can specify abilities based on actions on a resource) without actually having a model that represents that resource.

How do I define nested resources for backbone.js?

So I have a Rails 3.1 app that contains nested resources:
resources :projects do
resources :todos do
resources :tasks
end
end
I have defined my backbone.js models like:
var Task = Backbone.Model.extend({url:'/projects/1/todos/20/tasks'})
I can now create a new nested task as simply as:
task.set({description:"This is backbone.js created task!!!"})
task.save()
This, is pretty awesome.
However, note that I hard-coded the project/:project_id/todos/:todo_id/tasks url.
Of course, I can generate this dynamically but I was wondering if there was a better way.
Thanks for any suggestions.
Backbone.Model.extend is used to create subclasses, not objects, so creating a new class with a static URL and then instantiating it seems to be a particularly hairy method of going about things.
For problems like this, I'm very fond of Backbone Relational, which allows you to define a parallel set of structures as classes in Backbone, and have the Project object upload itself with all of its associated ToDo and Task objects. You would only ever send Projects as the RESTful "coarse document" you send to the client and receive from the client. See The Richardson Maturity Model for a discussion of REST, because backbone fully supports this particular model.
Another way is to SOAPly send change messages as updates, but that would take some hacking and understanding of Backbone's internal sync method.