Sinatra initialize a namespace controller - ruby

Under the commands folder, I'd like to initialize a controller
but Sinatra reports this error in the logs
ArgumentError: wrong number of arguments (given 1, expected 2)
/home/daniel/sinatraApi/app/commands/authenticate_user.rb:6:in `initialize'
/home/daniel/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rack-2.0.7/lib/rack/builder.rb:86:in `new'
2.0.7/lib/rack/builder.rb:147:in `to_app'
config.ru:15:in `inner_app'
/
The controllers are called
use AuthenticateUser
run ApplicationController
I've tried to add
use Commands::AuthenticateUser
or require
require "./app/commands/authenticate_user"
on config.ru
Authenticate_user
require 'jwt'
#require './app/controllers/application'
class AuthenticateUser
prepend SimpleCommand
def initialize(email, password)
#email = email
#password = password
end
def call
JsonWebToken.encode(user_id: user.id) if user
end
private
attr_accessor :email, :password
def user
user = User.find_by_email(email)
return user if user && user.authenticate(password)
errors.add :user_authentication, 'invalid credentials'
nil
end
end
How can I setup the namespace or nested controllers in Sinatra?

Related

Accessing an instance variable outside class with attr_reader

I've got a method sitting in a Services class. This method is going to take the name of a service and a key:value pair of an attribute I want to build a string query for to call out to the service i'm passing in.
I'm sending this build string query to the service via RestClient and capturing the response in a variable: #response
I want to carry this variable out of the Services class and use it. I've got attr_reader included in my class but i keep getting nil for #response when I try to access the response outside of Services.
What am I missing?
Example of my code:
class Services
attr_reader :response
def query_method(service,key,value)
where = "#{key}=#{value}"
#url = root_url + service + where
#response = RestClient::Request.execute(:method => :get, :url => #url)
end
end

Stubbing protected or private methods with Minitest

How can I stub private/protected methods to be passed in functional controller tests?
Having the following code for example:
app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
def create
#user = User.from_omniauth(auth_hash)
reset_session
session[:user_nickname] = #user.nickname
if #user.email.blank?
redirect_to edit_user_path(#user.nickname), :alert => "Please enter your email address."
else
redirect_to show_user_path(#user.nickname), :notice => 'Signed in!'
end
end
private
def auth_hash
request.env['omniauth.auth']
end
end
I tried the following :
test/controllers/sessions_controller_unit_test.rb
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
test "should create new user" do
# get the same 'request.env['omniauth.auth'] hash
auth_h = OmniAuth.config.mock_auth[:github]
# but auth_hash is never passed in User.find_or_create_from_auth_hash(auth_hash)
# method, which result to be nil breaking the User model call
get :create, provider: 'github', nickname: 'willishake', auth_hash: auth_h
assert_redirected_to show_user_path(nickname: 'willishake')
assert_equal session[:user_id], "willishake"
end
end
but when get :create (the test method) calls
the model User.find_or_create_from_auth_hash(auth_hash), auth_hash is nil, breaking the functional test.
So what's the right way to stub auth_hash private method and passing to User model call User.from_omniauth(auth_hash) ?
UPDATE:
after blowmage suggestion, it works like the following:
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
def setup
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:github]
end
test "should create new user" do
get :create, provider: 'github', nickname: 'willishake'
assert_equal session[:user_id], "willishake"
assert_redirected_to show_user_path(nickname: 'willishake')
end
end
Try this:
# set the request.env['omniauth.auth'] hash
auth_h = OmniAuth.config.mock_auth[:github]
request.env['omniauth.auth'] = auth_h

Rails - undefined method `authenticate'

I am receiving following error while trying to authenticate a user.
NoMethodError (undefined methodauthenticate' for #)`
user.authenticate('password') is successful(i.e returns the user object) when I execute the command from rails console.
irb(main):008:0> user.authenticate("sanket")
=> #<User id: 2, name: "Sanket", email: "sanket.meghani#infibeam.net", created_at: "2014-02-14 08:58:28", updated_at: "2014-02-14 08:58:28", password_digest: "$2a$10$KNuBgvqVDIErf3a24lMcaeNt1Hyg0I8oreIQSEYsXY4T...", remember_token: "3b64273a4fcced7c8bf91f7f7490e60a5919658d">
However when I put user.authenticate in a helper class, and access it using a url, it says
undefined method 'authenticate' for #<ActiveRecord::Relation:0x007fd9506dce38>
I am following Rails Tutorial
My user model looks like:
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
.
.
.
end
Related method in session_helper.rb looks like:
def create
user = User.where(:email => params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render "new"
end
end
It gives above error on if user && user.authenticate(params[:session][:password]) line.
What surprises me is, it is working from rails console, however the same call is not working from rails server.
As you can see in your error, where returns a relation, not a single object.
Check the related question: Why does a single record lookup return an array? (Rails beginner)
In your case, you can add first to the end of method chain, so it will return the first element of this relation:
user = User.where(:email => params[:session][:email].downcase).first

Validation not working on Class that extends ActiveModel::Naming, include ActiveModel::Validations

I and new to ruby and rails; I have the below ruby class definition that I am using in my rails 3 app. This class is simply used as a property container for contact information populated in my view on submit (form_for). I read a post where you can use ActiveModel directly apart from ActiveRecord, to perform validation, so I am trying it. I am getting the following exception when I check to see if the the object is valid? in my controller on postback. I assumed that valid? would be available being that I included ActiveModel::Validations; perhaps I am doing a few other things a$$ backwards. Any help would be appreciated:
undefined method `valid?' for #
Here's my class definition, further down is how I am handling it in my controller action:
require 'active_model'
class ContactModel
extend ActiveModel::Naming
include ActiveModel::AttributeMethods
include ActiveModel::Validations
include ActiveModel::Conversion
validates_presence_of :first_name, :last_name, :email_address, :email_address_confirmed, :subject, :contact_message
attr_accessor :first_name, :last_name, :email_address, :email_address_confirmed,
:telephone_number, :subject, :contact_message
Just messing around testing.
validates_each :first_name, :last_name do |record, attr, value|
record.errors.add attr, 'starts with z.' if value.to_s[0] == z
end
...
end
In my controller/action...
def send_email
##contact_model = ContactModel.new().initialize_copy(params[:contact_model])
#contact_model = params[:contact_model].dup
respond_to do |format|
if (#contact_model.valid?)
# Tell the UserMailer to send a welcome Email after save
ContactMailer.contact_email(#contact_model).deliver
format.html { redirect_to(#contact_model, notice: 'Email successfully sent.') }
format.json { render json: #contact_model, status: :created, location: #contact_model }
else
# What to do here?
end
end
end
In your controller you are setting #contact_model to a hash, params[:contact_model], and then calling valid? on it. You need create an instance of ContactModel and call valid on that. Like so:
#contact_model = ContactModel.new(params[:contact_model])
if (#contact_model.valid?)
...
I see commented out code that calls ContactModel.new(), but that's not how you want to do it anyway. Also, there is no reason to dup() or initialize_copy() on the params stuff.

Using Ability class in Rails Engine

I have a engine inside the lib folder named Support. In that folder, I have a Ticket controller.
I have created an ability class in the main app and I'm trying to manage all the models for the admin role. When I call the Tickets controller, it throws the error:
NameError in Support::TicketsController#index uninitialized constant Ticket
The app/model/ability.rb file is:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.role? == :admin
can :manage , :all
end
end
end
The lib/support/tickets_controller.rb file is:
module Support
class TicketsController < Support::ApplicationController
load_and_authorize_resource
respond_to :html, :xml, :json
def index
end
end
end
If the model class is namespaced differently than the controller, you will need to specify the :class option.
module Support
class TicketsController < ApplicationController
load_and_authorize_resource :class => Support::Ticket
end
end

Resources