rails mailer default :to - ruby-on-rails-3

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..

Related

GenericUser Laravel

Is there a way to overwrite the GenericUser class in Laravel?
Current authentication uses GenericUser as a return object for Auth:: operartions,
I want it to be my User class from my model. Is this feasible?
The source you're looking at is DatabaseUserProvider. This provider works without models and will be used if your auth driver (in app/config/auth.php) is set to database.
What you're actually using is the EloquentUserProvider which uses the model class defined in the config. The default is 'model' => 'User' which is the User model Laravel ships with.
You can change that value to any class name. But make sure to implement the necessary interfaces and add the traits. You basically need pretty much everything the default User model has.
Laravel already uses the default User model supplied as the authentication model.
You can customise this further in config\auth.php. The default is to use 'model' => 'User', - but you can change it to be something else.

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.

Get access to the logger object when not in a controller

I have a few business classes that represents logic of my application, I want to write logs from this classes, just like in a controller, how do I achieve reference to the logger.
If you want to tell me send it in constructor please don't.
When you know that your object will be in the Rails environment-- a service object, for example, instantiated and called from a controller --then you can use Rails.logger to get the same logger referenced by the logger method from within a controller or model.
For example, from app/services/payments.rb:
rescue Stripe::CardError => e
error = e.message
Rails.logger.error "Stripe card error for account #{#account}, error #{e}"
I am confused...I just use e.g.
logger.info("in model")
or
logger.info("in controller")
in my models and controllers all the time, this being rails 3.2/ruby 1.9.3. Of course I might be misunderstanding something completely.

Rails STI routing

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.

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.