In ActiveAdmin Gem, how to add invitation count to Admin model? - ruby-on-rails

In my project I have implemented activeadmin gem, which is integrated with devise invitable. So in my application admin can invite a customer through an email. While inviting, i'm getting the following error:
*ActiveRecord::StatementInvalid in Admin::CustomersController#send_invitation
Mysql2::Error: Unknown column 'invitations_count' in 'field list': UPDATE `admin_users` SET `invitations_count` = COALESCE(`invitations_count`, 0) + 1 WHERE `admin_users`.`id` = 1*

This issue has happened after updating the gem devise_invitable to version 1.3.3
To fix it just create a migration
rails g migration AddInvitationsCountToAdminUsers invitations_count:integer
That should add invitations_count field to the table admin_users
For others who has got the same problem but in another table. Here is a migration for a general table users
rails g migration AddInvitationsCountToUsers invitations_count:integer
Such issue could be caught by rspec
it 'user can be invited with passing current_user' do
current_user_attr = {email: 'user1#example.com', password: 'xxxxxx', password_confirmation: 'xxxxxx', ... add your fields }
user_attr = {email: 'user2#example.com', password: 'xxxxxx', password_confirmation: 'xxxxxx', ... add your fields }
current_user = User.create!(current_user_attr)
user = User.create!(user_attr)
user.invite!(current_user).class.should eq(Mail::Message)
end

Related

Rails 4.1 Devise 3.3 column users.password does not exist

I want to create a user manually through the console as such:
User.find_or_create_by(email: "user#mail.com", first_name: "Stan", last_name: "Smith", password: "password", password_confirmation: "password", confirmed_at: Time.now)
I have done this many times in past projects but this time it's not working. It is not picking up the Devise password model attribute so this is the error I get:
PG::UndefinedColumn: ERROR: column users.password does not exist
I have Rails 4.1.4 and Devise 3.3.0. Did something change in the latest versions?
Thanks.
Instead of User.find_or_create_by you should be using User.create.
Devise accepts a password and password confirmation on creation but the actual table only has a column called encrypted_password.
The "find" portion of User.find_or_create_by is looking for a column called "password" which doesn't exist.
Still if you want to use find_or_create_by, it accepts block:
User.find_or_create_by(email: "user#mail.com", first_name: "Stan", last_name: "Smith") do |user|
user.password = "password"
user.confirmed_at = Time.now
end

Rails devise customization

In devise's user table i'm trying to save email and password through a different controller , but in server log i'm getting this error ,
WARNING: Can't mass-assign protected attributes for User: encrypted_password
------------------------------------------------------------------------------
(0.1ms) BEGIN
User Exists (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'mail#mail.com' LIMIT 1
(0.2ms) ROLLBACK
Added encrypted_password attribute to the model.
So i'm not able to insert it to users table.
And ,How to encrypt password? as devise gem has built in.
Controller :
#user = User.new(:email => params[:supplier][:username],:username => params[:supplier][:username],:encrypted_password => '$2a$10$/Hh7UegsWm2ndRXbDEfjvOKBUiI1fa/z1GIbE5OYFpyqW6P97llsG')
#user.save
puts #user.errors
Model :
attr_accessible :encrypted_password
I would simply use
user = User.new(email: "...", password: "password", password_confirmation: "password")
user.skip_confirmation!
user.save!
This way, devise will do the work for you, and you don't have to bother about confirmation emails. If you want confirmation emails still to be send out, remove the second line

Select when devise send the confirmation mail or not

i need to select when devise send the confirmation mail. I overwrite the send_confirmation_instructions and add a date field named invited on my user model. If the invited field is null send the mail, otherwise not sent.
Here's my code:
def send_confirmation_instructions
unless invited.nil?
self.confirmation_token = nil if reconfirmation_required?
#reconfirmation_required = false
generate_confirmation_token! if self.confirmation_token.blank?
else
self.confirmation_token = nil if reconfirmation_required?
#reconfirmation_required = false
generate_confirmation_token! if self.confirmation_token.blank?
self.devise_mailer.confirmation_instructions(self).deliver
end
end
The output of my console its:
Loading development environment (Rails 3.0.10)
1.9.3-p0 :001 > User.create!(:email => "hello#test.com") NoMethodError: undefined method `reconfirmation_required?' for
I have rewrite many other devise method without problems. Any idea ?
Thanks in advance.
The best solution was to connect manually to the database and add the fields to the users table.
# read configuration from database.yml
config = Rails.application.config.database_configuration[Rails.env]
host = config["host"]
database = config["database"]
username = config["username"]
password = config["password"]
# stablish db connection
mysql = Mysql2::Client.new(:host => host, :username => username, :database => database, :password => password)
# generate devise confirmation token
token = (Digest::MD5.hexdigest "#{ActiveSupport::SecureRandom.hex(10)}-#{DateTime.now.to_s}")
# made the sql
sql = "INSERT INTO users (email, confirmation_token, confirmation_sent_at) VALUES ('email#test.com', '#{token}', '#{Time.now.to_s}')"
# execute query
mysql.query(sql)
# close mysql connection
mysql.close
I hope this will be useful.

How do you add an administratior in a rails 3 appliction

I have just built a rails 3 application by using Mike Hartl's "Learn Rails by Example". I am ready to deploy it but I am confused about how to add the administrator to the application. I will be the only administrator. Will the administrator be added before deployment and if so how do I do this.
What I believe you need when you talk about an "administrator account" is in fact two different things: authentication (the login) & authorization (what a login can/cannot do).
Under rails, one way to do that is by using two different gems. I suggest you have a look at devise, and cancan. They have both been developed and are actively maintained by rails superstars: José Valim and Ryan Bates.
The tutorial doesn't actually go through creating an interface where you can create admins. If you want to test the part where you're not allowed to delete other administrator accounts, you can test it with faker by adding 2 admins to the sample_data.rake file:
def make_users
admin = User.create!(:name => "Example User",
:email => "example#railstutorial.org",
:password => "foobar",
:password_confirmation => "foobar")
admin.toggle!(:admin)
admin2 = User.create!(:name => "Example User2",
:email => "example2#railstutorial.org",
:password => "foobar",
:password_confirmation => "foobar")
admin.toggle!(:admin)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
If you want to add an admin to production, I'm guessing you could create your account and toggle the admin function with a database editor and then push the db to the production server? That's what I would do but I'm by no means an expert.
for admin control panel on my web-apps i'm using typus gem https://github.com/fesplugas/typus/
it will generate an admin page and by the default typus will use your model default_scope to fetch data.
I'm busy with the same thing!
I found this on STACK:
rails c
Loading development environment (Rails 3.0.0.beta3)
irb(main):001:0> admin = Admin.create! do |u|
irb(main):002:1* u.email = 'sample#sample.com'
irb(main):003:1> u.password = 'password'
irb(main):004:1> u.password_confirmation = 'password'
irb(main):005:1> end
I changed the Admin to User, but the problem is it creates a normal user not an admin user. Somewhere we need to put in - admin.toggle!(:admin) or make it true. I'll let you know if I find anything else.

Testing Rails Controllers Inherited from Typus

I've been fighting with this for a couple of days and there doesn't seem to be much help online. I've looked at the Typus wiki, sample app, and tests and I appear to be doing things correctly but I stil get HTTP Status Code 302 (Redirect) where I expect 200 (Success) in my tests.
Below are what should be the appropriate files (with irrelevant stuff removed)
config/initializers/typus.rb (rails g typus:migration has been run as I have an admin_users table):
Typus.setup do |config|
# Application name.
config.admin_title = "Something"
# config.admin_sub_title = ""
# When mailer_sender is set, password recover is enabled. This email
# address will be used in Admin::Mailer.
config.mailer_sender = "noreply#somewhere.com"
# Define paperclip attachment styles.
# config.file_preview = :medium
# config.file_thumbnail = :thumb
# Authentication: +:none+, +:http_basic+
# Run `rails g typus:migration` if you need an advanced authentication system.
config.authentication = :session
# Define user_class_name.
config.user_class_name = "AdminUser"
# Define user_fk.
config.user_fk = "admin_user_id"
# Define master_role.
config.master_role = "admin"
end
config/typus/admin_user.yml
AdminUser:
fields:
default: first_name, last_name, role, email, locale
list: email, role, status
form: first_name, last_name, role, email, password, password_confirmation, locale
options:
selectors: role, locale
booleans:
status: Active, Inactive
filters: status, role
search: first_name, last_name, email
application: Admin
description: Users Administration
test/factories/admin_users.rb:
Factory.define :admin_user do |u|
u.first_name 'Admin'
u.last_name 'User'
u.email 'admin#somewhere.com'
u.role 'admin'
u.password 'password!'
u.token '1A2B3C4D5E6F'
u.status true
u.locale 'en'
end
test/functional/admin/credits_controller_test.rb:
require 'test_helper'
class Admin::CreditsControllerTest < ActionController::TestCase
setup do
#admin_user = Factory(:admin_user)
#request.session[:admin_user_id] = #admin_user.id
#request.env['HTTP_REFERER'] = '/admin/credits/new'
end
context "new" do
should "be successful" do
get :new
assert_response :success
end
end
end
#response.body:
<html>
<body>You are being redirected.
</body>
</html>
As you can see, I've set up the typus to use admin_user and admin_user_id for the session key. But for some reason that test fails getting 302 rather than 200. I'm sure this is because I'm doing something wrong that I just don't see. I've also created all these a gist, just in case someone prefers that.
Edited 2011-05-19 09:58am Central Time: Added Response body text per request.
I figured this out. It was a problem with the config/typus/admin_roles.yml file.
Before:
admin:
Category: create, read, update
Credit: read
...
After:
admin:
Category: create, read, update
Credit: read, create
...
The problem was that admin users didn't have access to the CREATE action on the admin/credits_controller which resulted in the user being sent back to the admin login address.
Giving admin users access to the action and changing the
#session[:admin_user_id]
to
#session[:typus_user_id] #Just like in the Typus docs
solved the problem. I had changed it to :admin_user_id because of the
config.user_fk = "admin_user_id"
in the typus config files, while trying to troubleshoot this issue.

Resources