Custom authentication for Blazer (Rails) - ruby-on-rails

Where do I define authenticate! to get custom authentication for Blazer?
I have this (uncommented) line in my config/blazer.yml (as described in the README under Authentication > Other):
before_action: :authenticate!
When I refresh my app, I get this error:
NoMethodError at / undefined method 'authenticate!' for #<Blazer::QueriesController:0x007ffe26447830>
That class is defined by the Blazer gem. Am I supposed to add/redefine its authenticate! method somehow?
I tried adding an initializers/blazer.rb file where I defined:
class Blazer::QueriesController < Blazer::BaseController
def authenticate!
true
end
end
But now, when I click "New Query" in Blazer, I get this error:
NoMethodError in Blazer::Queries#new undefined method `errors' for nil:NilClass
Rails 4.2.5, Blazer 1.8.0

You should add your custom method into application_controller.rb. You then put the name of the method into config/blazer.yml to wire it up.
I would recommend using Devise for authentication. The Devise wiki has a great starting article here. The engine and routes of blazer must also be protected as demonstrated here.

Related

Why can't I have a mailer method named message?

I've created a new mail on rails 5 using the mailer generator:
$ rails g mailer mymailer message
Rails created the application_mailer, mymailer_mailer, views and tests. Ok.
This is the mailer generated by rails:
class MymailerMailer < ApplicationMailer
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.mymailer_mailer.message.subject
#
def message
#greeting = "Hi"
mail to: "to#example.org"
end
end
But whenever I've tried to send the mail I've got the following error:
NoMethodError: undefined method `reject!' for nil:NilClass
After spent about two hours double-checking every config file I've decided to change method to bla...
VoilĂ : It worked, Ok! But why?
BTW: The message method I've found is from ActionMailer::MessageDelivery but there's no mention on Rails Guides of that.
If you look at the docs for MessageDelivery, there appears to be a method already provided named message which
Returns the resulting Mail::Message
My assumption is that your definition is overriding this provided method, but you are not returning the expected Mail::Message type object.
As another answer stated, there's already a method in the class named message. This shouldn't be a problem if you use the class as intended, since the mailer shouldn't have a single message named "message", it should have a more descriptive name.
The intent of a Mailer object is to define a context for messages that may be sent.
So for example, a UserMailer would be used to build messages to a user. Then each different type of message has a method, such as forgotten_password or welcome.
The documentation includes a more thorough example that follows this.

Why I'm getting undefined method `cookies'?

I'm trying to setup Authlogic with single_access_token for my rails backend app (I'm using rails-api gem).
I'm following this example: http://blog.centresource.com/2013/06/04/using-ember-auth-with-rails-3-and-authlogic/
So far, I'm able to create users, but when I try to login it fails on user_session.save:
NoMethodError in Api::V1::UserSessionsController#create
undefined method `cookies' for #<Api::V1::UserSessionsController:0x00000004528cb8>
According to similar questions I've found here, I have to configure Authlogic as:
acts_as_authentic do |c|
c.login_field = :email
c.maintain_sessions = false
end
on my User model. However, it keeps failing. I'm assuming when UserSession is saved, Authlogic tries to update the magic fields on User, but I have no idea why is still trying to use cookies.
The tutorial assumes a regular rails app, but you're using rails-api. By default controllers in rails-api don't include the middleware that handles cookies. If you need cookies then you need to add that middleware:
config.middleware.use ActionDispatch::Cookies
And then include ActionController::Cookies in your controller. There's more info on what's included by default in the rails-api README
Rails-api does have additional steps to accept cookies
add config.middleware.use ActionDispatch::Cookies to config/application.rb
add at the top under class include ActionController::Cookies in your expected controller
Restart the application

Rails -> Devise -> how to access current_user from Module

I'm currently using Rails 3.2.9 with devise 3.0.4
I can access Devise current_user in Controller without any problem.
However, when I'm trying to access current_user in Module as below and receive error.
c_time = DateTime.current.in_time_zone(current_user.local_timezone)
NameError (undefined local variable or method `current_user' for DateCalculator:Module):
It would be great if someone please advise what's the best way to access current_user from Module.
Many Thanks in advance.
Try this one instead of using current_user object
UserSession.find.user
If your module does something specific to current signed in user, your can follow this way
config/initializers/user_specific_time.rb
module Devise
module Controllers
module Helpers
def user_specific_time
c_time = DateTime.current.in_time_zone(current_user.local_timezone)
end
end
end
end
There is a Gem called sentient_user it is a cheeky approach but it does exactly what you need ;) which is to have access to current logged in user, from view | Controller | Model etc
https://rubygems.org/gems/sentient_user

cant get cancan to work on rails 3.1

I had a two month break using Rails and now its biting me when I return!
I am trying to get CanCan to work on Rails 3.1 and have viewed the railscast and then followed their instructions on the cancan git page. However when I try run the homepage it gives me the following error:
Routing Error
undefined local variable or method `authorize_resource' for StaticController:Class
How do I make this stop? Its as if cancan is not loaded, but I have installed it using bundler and it doesnt mention that I have to do anything else to include it.
Anyone have any ideas?
Sorry my bad! Too much coffee resulted in me not reading all the instructions for how to install it.
I needed to add the following to my ApplicationController:
def current_ability
#current_ability ||= Ability.new(current_user)
end
and then I needed to use
class StaticController < ApplicationController
authorize_resource :class => false
Because in this particular example it was just a static simple homepage that sits infront of a more complicated web app.
Thanks for the help.

RSpec accessing application_controller methods such as 'current_user'

I'm trying to stub out a method on my current_user (using a modified restful_authentication auth solution) with rspec. I'm completely unsure of how I can access this method in my controller specs. current_user by itself doesn't work. Do I need to get the controller itself first? How do I do this?
Using rails 2.3.5, rspec 1.3.0 and rspec-rails 1.3.2
# my_controller_spec.rb
require 'spec_helper'
describe MyController do
let(:foos){ # some array of foos }
it "fetches foos of current user" do
current_user.should_receive(:foos).and_return(foos)
get :show
end
end
Produces
NoMethodError in 'ChallengesController fetches foos of current user'
undefined method `current_user' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1::Subclass_1::Subclass_2::Subclass_2:0x7194b2f4>
rspec-rails gives you a controller method for use in controller examples. So:
controller.stub!(:current_user).with(:foos).and_return(foos)
ought to work.
how can it know where to find current_user? this should solve it:
subject.current_user.should_receive(:foos).and_return(foos)
I'm not entirely familiar with restful_authentication, just Authlogic and Devise, but it's probably similar in that current_user is a controller method and not an object, which is why calling should_receive on it isn't working as expected (you're setting an expectation on the object that current_user returns, but the method isn't accessible inside the scope of your expectation).
Try this:
stub!(:current_user).and_return(foos)
I read this and tweaked mine a bit. If you simply want to pass in a user object into your Rspec test, you can use this:
First, create a user object within the rspec test. For example:
(use whatever attributes you need or are required to create the user object.)
user = User.create(name: "ted")
(Note: you can also use a factory from FactoryGirl.)
Now, with that user object which is saved into the variable "user", do this within that same Rspec test:
controller.stub!(:current_user).and_return(user)
that should work...

Resources