I'm wondering how I can go about something like this in the devise config.rb.
if request.subdomain == 'app'
config.case_insensitive_keys = [:username]
elsif request.subdomain == 'support'
config.case_insensitive_keys = [:email]
end
The use here is that I want system users to only be able to login with their employee_id's (username) because their email addresses can appear across multiple accounts so I needed a way to log them in and register without the devise email validations catching them, however I want the backend group to login only by email (me and future devs) as there should never be more than one user there with multiple emails
this is failing when I try to drop my database for some reason?
STACK TRACE
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type.rb:27: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type/adapter_specific_registry.rb:9: warning: The called method `add_modifier' is defined here
rails aborted!
SyntaxError: /Users/taurenltd/Desktop/Development/roadze-base/config/initializers/devise.rb:301: syntax error, unexpected end-of-input, expecting `end'
/Users/taurenltd/Desktop/Development/roadze-base/config/environment.rb:5:in `<main>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/rails:9:in `<top (required)>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:drop => db:load_config => environment
(See full trace by running task with --trace)
taurenltd#Shawns-MBP roadze-base % rails db:drop db:create db:migrate
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type.rb:27: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/taurenltd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type/adapter_specific_registry.rb:9: warning: The called method `add_modifier' is defined here
rails aborted!
NameError: undefined local variable or method `request' for main:Object
Did you mean? require
/Users/taurenltd/Desktop/Development/roadze-base/config/initializers/devise.rb:55:in `block in <main>'
/Users/taurenltd/Desktop/Development/roadze-base/config/initializers/devise.rb:5:in `<main>'
/Users/taurenltd/Desktop/Development/roadze-base/config/environment.rb:5:in `<main>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/rails:9:in `<top (required)>'
/Users/taurenltd/Desktop/Development/roadze-base/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Got this working by using the following block
def self.matches?(request)
if request.subdomain.present? && request.subdomain == 'app'
config.case_insensitive_keys = [:username]
elsif request.subdomain.present && request.subdomain == 'support'
config.case_insensitive_keys = [:email]
end
end
Related
I am trying to run a single test file: bundle exec rspec spec/models/user_spec.rb
But get following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller
NameError:
uninitialized constant Rails::Controller
Did you mean? ApiController
# ./spec/rails_helper.rb:149:in `block in <top (required)>'
# ./spec/rails_helper.rb:61:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
Initially, I was getting following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: module Shoulda::Matchers::ActiveModel
NameError:
uninitialized constant Shoulda
# ./spec/support/matchers/validate_kept_of_matcher.rb:4:in `<top (required)>'
# ./spec/rails_helper.rb:51:in `block in <top (required)>'
# ./spec/rails_helper.rb:51:in `each'
# ./spec/rails_helper.rb:51:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
But then it fixed after I added following to the test file.
require "shoulda/matchers"
I am newbie in Ruby/Rails world, can someone please give a direction?
Rails::Controller::Testing::TemplateAssertions was removed in Rails 5.
You can re-add the depreciated functionality by installing the Rails controller testing gem. However the use of controller specs, assigns and template assertions is discouraged by both the RSpec and Rails teams and is not very future proof.
The community accepted solution is to write request specs and stop poking inside your controllers.
I got a database to seed to test my code. When I run rake db:seed to seed the database, I get the error below.
I expected the console to be blank: this means the data has been seeded.
Error:
[19:19:41] (master) simple-partials-lab-noukod-000
// ♥ rake db:seed
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-4.2.11.1/lib/active_support/core_ext/object/duplicable.rb:111: warning: BigDecimal.new is deprecated; use BigDecimal() method instead.
rake aborted!
ArgumentError: missing keywords: from, to
/usr/local/rvm/gems/ruby-2.6.1/gems/faker-2.2.1/lib/faker/default/date.rb:6:in `between'
/home/considerate-transform-1229/simple-partials-lab-noukod-000/db/seeds.rb:9:in `block in <top (required)>'
/home/considerate-transform-1229/simple-partials-lab-noukod-000/db/seeds.rb:8:in `times'
/home/considerate-transform-1229/simple-partials-lab-noukod-000/db/seeds.rb:8:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.6.1/gems/railties-4.2.11.1/lib/rails/engine.rb:547:in `load_seed'
/usr/local/rvm/gems/ruby-2.6.1/gems/activerecord-4.2.11.1/lib/active_record/tasks/database_tasks.rb:253:in `load_seed'
/usr/local/rvm/gems/ruby-2.6.1/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.6.1/gems/rake-12.3.3/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.6.1/bin/ruby_executable_hooks:24:in `eval'
/usr/local/rvm/gems/ruby-2.6.1/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
Looks like you're using Faker with the old syntax. It recently changed to require keyword arguments rather than positional arguments.
You need to follow the updated syntax:
Faker::Date.between(from: 2.days.ago, to: Date.today) #=> "Wed, 24 Sep 2014"
Note the keywords from: and to: which specify the arguments. Order of arguments is not important, and the reverse order will work just the same:
Faker::Date.between(to: Date.today, from: 2.days.ago) #=> "Wed, 24 Sep 2014"
Additional information available in their docs: https://github.com/faker-ruby/faker/blob/master/doc/default/date.md
I have a rake task which is called by a third party service. In my application I need to set Customer before starting the application. I could not access the model variable from the rake task. But this happens only in Production not in Development. When I run
RAILS_ENV = 'production' rake auth:tester
I get the followng error.
rake aborted!
Unable to autoload constant CUSTOMER, expected /home/suganya/academics/tsg-mcds-server/app/models/customer.rb to define it
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:495:in `load_missing_constant'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:184:in `const_missing'
/home/suganya/academics/tsg-mcds-server/config/initializers/customer_config.rb:11:in `xml'
/home/suganya/academics/tsg-mcds-server/config/initializers/customer_config.rb:38:in `set_customer_settings'
/home/suganya/academics/tsg-mcds-server/config/initializers/customer_config.rb:710:in `<top (required)>'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `block in load'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.2.7.1/lib/rails/engine.rb:652:in `block in load_config_initializer'
I have referred all the related questions. When I enable in poduction.rb
config.threadsafe!
config.dependency_loading = true if $rails_rake_task
I get the following error.
undefined method `threadsafe!' for #<Rails::Application::Configuration:0x00000004aebdf8>
Also I tried
Rails.application.eager_load!
this is my rake task:
namespace :authentication do
desc "Automatically runs authentication tester"
task :tester => :environment do
Rake.application.rake_require "#{Rails.root}/config/initialize_customer_in_development.rb"
begin
AuthenticationTester.perform
rescue
SystemEvent.error(50049, "Authentication Tester Finished Unsuccessfully.")
end
end
end
Nothing helped. please help to solve this problem.
When I do rake db:migrate I get this error:
rake db:migrate
rake aborted!
NoMethodError: undefined method `each' for false:FalseClass
/home/chirag/social-login-in-rails/config/application.rb:25:in `<class:Application>'
/home/chirag/social-login-in-rails/config/application.rb:10:in `<module:SociaLoginRails>'
/home/chirag/social-login-in-rails/config/application.rb:9:in `<top (required)>'
/home/chirag/social-login-in-rails/Rakefile:4:in `require'
/home/chirag/social-login-in-rails/Rakefile:4:in `<top (required)>'
/home/chirag/.rvm/gems/ruby-2.0.0-p0/bin/ruby_executable_hooks:15:in `eval'
/home/chirag/.rvm/gems/ruby-2.0.0-p0/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
I have cloned this app :- https://github.com/mohitjain/social-login-in-rails
Rename the file 'social_keys_example.yml' to 'social_keys.yml' and put your app keys then restart the sever, it will work
in config/application.rb
change
social_keys = File.join(Rails.root, 'config', 'social_keys.yml')
to
social_keys = File.join(Rails.root, 'config', 'social_keys_example.yml')
follow the instructions
http://codebeerstartups.com/2013/10/social-login-integration-with-all-the-popular-social-networks-in-ruby-on-rails/
In the config directory there is a file ie social_keys.yml. You need
to specify all the social keys there. Make sure have restarted the
application after making any change in that file.
Here are the urls from where you can create an app and get the keys
In my rails app, I use the gem i18n_spec to add rspec tests that check if my i18n config files are correct.
The spec file
require 'spec_helper.rb'
require 'i18n-spec/tasks'
Dir.glob('config/locales/*.yml') do |locale_file|
describe "#{locale_file}" do
it { subject.should be_parseable }
results in errors
../gems/i18n-spec-0.6.0/lib/i18n-spec/tasks.rb:5:in `<top (required)>': undefined method `namespace' for main:Object (NoMethodError)
from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require'
from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require_with_backports'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `block in require'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:236:in `load_dependency'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `require'
from ../spec/selftest/i18n_spec.rb:3:in `<top (required)>'
Why do you want to test if they are parseable? If you are asserting any I18n key in a unit / feature spec Rails will load your I18n files and throw a syntax error if the yml files aren't parseable!
You will get assertions like so:
within(".mod-header .login") { click_link I18n.t('public.menu.login') }
find("#main-container h1").should have_content I18n.t('pages.login.title')
This way Rails will load and parse your yml files. And if there are any errors you will find them in your test output!
And errors like this:
<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): mapping values are not allowed in this context at line <num> column <num>>
or
<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): did not find expected key while parsing a block mapping at <num> column <num>>