Invite user without email + rails + devise invitable - ruby-on-rails-5

I want to have a feature where I can invite any user using only their phone_number. I don't have the email address. When a user is being invited, an SMS is sent to the specified number and depending upon the contents of the reply(say, 'Y' or 'N') SMS from user, their account is confirmed/rejected. We plan to provide a link in the sms where in the user can click and enter the required details(email, name etc).
Loading development environment (Rails 5.0.2)
2.3.0 :001 > u = User.invite!(phone_number: '1234567890')
=> #<User id: nil, email: nil, first_name: nil, last_name: nil, phone_number: "1234567890", created_at: nil, updated_at: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_type: nil, invited_by_id: nil, invitations_count: 0>
2.3.0 :002 > u.errors
=> #<ActiveModel::Errors:0x007fc91fcd4c00 #base=#<User id: nil, email: nil, first_name: nil, last_name: nil, phone_number: "1234567890", created_at: nil, updated_at: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_type: nil, invited_by_id: nil, invitations_count: 0>, #messages={:email=>["can't be blank"]}, #details={:email=>[{:error=>:blank}]}>

I have same problem and i set example email for it and remove it later...
u = User.invite!(phone_number: '1234567890', email: 'example#abc.com', skip_invitation: true)
u.update(email: '')
Add this to user.rb:
def email_required?
false
end
def email_changed?
false
end

Related

Testing controller new action with Rspec2 + Rails3

I have the following rspec2 test case for my controller new action
describe "GET #new" do
it "should assign new project to #project" do
project = Project.new
get :new
assigns(:project).should eq(project)
end
end
and I'm getting the following error
1) ProjectsController GET #new should assign new project to #project
Failure/Error: assigns(:project).should eq(project)
expected: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil>
got: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil>
(compared using ==)
Diff:#<Project:0x007f461c498270>.==(#<Project:0x007f461c801c90>) returned false even though the diff between #<Project:0x007f461c498270> and #<Project:0x007f461c801c90> is empty. Check the implementation of #<Project:0x007f461c498270>.==.
# ./spec/controllers/projects_controller_spec.rb:19:in `block (3 levels) in <top (required)>'
Finished in 1.21 seconds
13 examples, 1 failure, 10 pending
Failed examples:
rspec ./spec/controllers/projects_controller_spec.rb:16 # ProjectsController GET #new should assign new project to #project
and when I use == instead on eq, I'm getting the following error
1) ProjectsController GET #new should assign new project to #project
Failure/Error: assigns(:project).should == project
expected: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil>
got: #<Project id: nil, name: nil, company_id: nil, created_at: nil, updated_at: nil> (using ==)
Diff:#<Project:0x007f461c4f5420>.==(#<Project:0x007f461c63b280>) returned false even though the diff between #<Project:0x007f461c4f5420> and #<Project:0x007f461c63b280> is empty. Check the implementation of #<Project:0x007f461c4f5420>.==.
what am I doing wrong here, thanks in advance
I'm on
Rails3
Rspec2
You are creating a new project before accessing the new action, this is unnecessary. Your controller actually does that work for you already. The problem you are facing is that you have two new projects created (in your case you have created Project:0x007f461c498270 first and then Project:0x007f461c801c90, they have the same attributes but are different projects). This test should pass:
describe "GET #new" do
it "assigns a new Project to #project" do
get :new
assigns(:project).should be_a_new(Project)
end
end

Testing rails controller with rspec: value is same, but object id is different

I try to test a rails controller with rspec like this:
require 'spec_helper'
describe NewsController do
describe "GET 'edit'" do
before(:each) do
#news_1 = FactoryGirl.create(:news_1)
get :edit, { :id => #news_1.id }
end
it { response.should be_success }
it { assigns(:news).should eq(#news_1) }
it { response.should render_template(:edit) }
end
end
But, I got this error.
Failures:
1) NewsController GET 'edit'
Failure/Error: it { assigns(:news).should eq(#news_1) }
expected: #<News id: 1, title: "news_title_1", contents: "news_contents_1", display: nil, created_at: "2012-11-29 07:24:49", updated_at: "2012-11-29 07:24:49", news_date: nil, orion: false, pw: false, op: false, pickup: false, info_1: nil, info_2: nil, info_3: nil, info_4: nil, info_5: nil, del: false, place: nil, contact: false>
got: #<News id: 1, title: "news_title_1", contents: "news_contents_1", display: nil, created_at: "2012-11-29 07:24:49", updated_at: "2012-11-29 07:24:49", news_date: nil, orion: false, pw: false, op: false, pickup: false, info_1: nil, info_2: nil, info_3: nil, info_4: nil, info_5: nil, del: false, place: nil, contact: false>
(compared using ==)
Diff:#<News:0x000001030b55c8>.==(#<News:0x000001033126b8>) returned false even though the diff between #<News:0x000001030b55c8> and #<News:0x000001033126b8> is empty. Check the implementation of #<News:0x000001030b55c8>.==.
# ./spec/controllers/news_controller_spec.rb:73:in `block (3 levels) in <top (required)>'
I think these values are same, but object id is different.
So this test fails...
How do I solve this error?
Had this problem myself. My workaround was comparing the attributes, i.e.
it { assigns(:news).attributes.should eq(News.last.attributes) }
I think the problem here is how the 'eq' does the comparison between two objects. you might want to use == in this scenario. You can see a detailed explanation in this question here
you should do as follow
require 'spec_helper'
describe NewsController do
describe "GET 'edit'" do
before(:each) do
#news_1 = FactoryGirl.create(:news_1)
get :edit, { :id => #news_1.id }
end
it { response.should be_success }
it { assigns(:news).should eq(News.last) }
it { response.should render_template(:edit) }
end
end
== and eq are different. To test your method when it retrieves the object, then use should == or its equivalent a.should eql
I had this problem comparing an array and I solved it with:
expect(assigns(:receipts)).to match_array(receipts)

When seeding data for devise user model password is empty

In my seeds file I have the following:
puts "Creating Deans User"
user = User.create!(:email => "test#test.com", :password => "test1234", :password_confirmation => "test1234", :name => "Dean Chester", :admin => true)
puts "User created"
But when I check this in the console I see the following:
[#<User id: 1, email: "test#test.com", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2012-08-06 10:02:22", updated_at: "2012-08-06 10:02:22", admin: true, name: "Dean Chester">]
And the encrypted password field is blank so what is going wrong?
Do you have:
attr_accessor :password, :password_confirmation, :current_password
in your model? If so remove it and it will fix your issue.
You might be missing attr_accessible :password, :password_confirmation in your `User.rb.

setting an atttribute in rails console - transaction rolled back

I've used this forum gem before and also been able to set the forem_admin to "true" using the console. However, this time it's rolling back the transaction when I try to save.
You can see below that I set the forem_admin to "true," saved it, it rolled back the transaction (which means it didn't save), I did "u" and it showed forem_admin = true but when I restarted the console it showed forem_admin = false.
If anyone's familiar with this gem, I think this forem_state attribute is new, so I'm wondering if anything's changed.
I'm not an SQL pro but one thing that looks funny to me is the line
"users"."id" != 5
I would have expected it to have been "users"."id" = 5 i.e. with no "!", because the id of the user "signuplinks" is 5.
this is a rails 3.2 app
update when I do u.errors after u.save, I'm getting this error message
#messages={:password=>["can't be blank"]}
Console
ruby-1.9.3-rc1 :001 > u = User.last
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 5, email: "myemail#gmail.com", encrypted_password: "$2a$10$axjwcO.kU4/mqC9Llyj.b.r/2jJULnWKmG7Pi3Zu1AE3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-03-05 03:37:59", last_sign_in_at: "2012-03-05 03:37:59", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-03-05 03:37:58", confirmation_sent_at: "2012-03-05 03:34:33", unconfirmed_email: nil, created_at: "2012-03-05 03:34:33", updated_at: "2012-03-05 03:37:59", name: "signuplinks", country: "Canada", image: "3.png", forem_admin: false, forem_state: "pending_review">
ruby-1.9.3-rc1 :002 > u.forem_admin = true
=> true
ruby-1.9.3-rc1 :003 > u.save
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 FROM "users" WHERE (LOWER("users"."name") = LOWER('signuplinks') AND "users"."id" != 5) LIMIT 1
User Exists (0.1ms) SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('signuplinks#gmail.com') AND "users"."id" != 5) LIMIT 1
(0.1ms) rollback transaction
=> false
ruby-1.9.3-rc1 :004 > u
=> #<User id: 5, email: "myemail#gmail.com", encrypted_password: "$2a$10$axjwcO.kU4/mqC9Llyj.b.r/2jJULnWKmG7Pi3Zu1AE3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-03-05 03:37:59", last_sign_in_at: "2012-03-05 03:37:59", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-03-05 03:37:58", confirmation_sent_at: "2012-03-05 03:34:33", unconfirmed_email: nil, created_at: "2012-03-05 03:34:33", updated_at: "2012-03-05 03:37:59", name: "signuplinks", country: "Canada", image: "3.png", forem_admin: true, forem_state: "pending_review">
ruby-1.9.3-rc1 :005 > exit
Michael-Pro:ic michl$ rails c
Loading development environment (Rails 3.2.1)
ruby-1.9.3-rc1 :001 > u = User.last
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 5, email: "myemail#gmail.com", encrypted_password: "$2a$10$axjwcO.kU4/mqC9Llyj.b.r/2jJULnWKmG7Pi3Zu1AE3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-03-05 03:37:59", last_sign_in_at: "2012-03-05 03:37:59", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-03-05 03:37:58", confirmation_sent_at: "2012-03-05 03:34:33", unconfirmed_email: nil, created_at: "2012-03-05 03:34:33", updated_at: "2012-03-05 03:37:59", name: "signuplinks", country: "Canada", image: "3.png", forem_admin: false, forem_state: "pending_review">
actually, this was my own fault. Thanks to #muistooshort for pointing out problem probably is in model. I had included "password" in the validates_presence of validator which was stopping me from changing user details in the console (without including password)

Rails 3 routing error with namespace and uncountable noun together

Well this is my first time doing this. In routes.rb I have the following:
namespace :admin do
resources :news
end
And I have this after rake routes:
admin_news_index GET /admin/news(.:format) admin/news#index
POST /admin/news(.:format) admin/news#create
new_admin_news GET /admin/news/new(.:format) admin/news#new
edit_admin_news GET /admin/news/:id/edit(.:format) admin/news#edit
admin_news GET /admin/news/:id(.:format) admin/news#show
PUT /admin/news/:id(.:format) admin/news#update
DELETE /admin/news/:id(.:format) admin/news#destroy
As you can see the path to the action "new" is new_admin_news_path, unfortunately, when I visited that path, something pop up as follow:
No route matches {:action=>"show", :controller=>"admin/news", :id=>#<News id: nil, news_type_id: nil, title: nil, content: nil, views: nil, status: nil, start_date: nil, end_date: nil, created_at: nil, updated_at: nil, news_key: nil>}
I was thinking that the "news" is an uncountable noun and this might be the problem. So I change the config/initializers/inflections.rb to:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w( fish sheep news )
end
Obviously, it is not working...
What should I do? Any suggestions?