From 9af1f2f4a9b4acdb8bd17d72ef3385e6bc978859 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 01:00:46 +0200 Subject: [PATCH] Adds registration controller for organizations --- .../organizations/registrations_controller.rb | 29 +++++++++ .../organizations/registrations/new.html.erb | 64 +++++++++++++++++++ config/locales/activerecord.en.yml | 5 +- config/locales/activerecord.es.yml | 5 +- config/locales/devise_views.en.yml | 12 ++++ config/locales/devise_views.es.yml | 12 ++++ config/routes.rb | 5 ++ spec/features/organizations_spec.rb | 32 ++++++++++ 8 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 app/controllers/organizations/registrations_controller.rb create mode 100644 app/views/organizations/registrations/new.html.erb create mode 100644 spec/features/organizations_spec.rb diff --git a/app/controllers/organizations/registrations_controller.rb b/app/controllers/organizations/registrations_controller.rb new file mode 100644 index 000000000..2751bc988 --- /dev/null +++ b/app/controllers/organizations/registrations_controller.rb @@ -0,0 +1,29 @@ +class Organizations::RegistrationsController < Devise::RegistrationsController + include RecaptchaHelper + + def new + super do |user| + user.build_organization + end + end + + def create + if verify_captcha?(resource) + super do |user| + # Removes unuseful "organization is invalid" error message + user.errors.messages.delete(:organization) + end + else + build_resource(sign_up_params) + flash.now[:alert] = t('recaptcha.errors.verification_failed') + respond_with resource + end + end + + private + + def sign_up_params + params.require(:user).permit(:email, :password, :phone_number, :password_confirmation, organization_attributes: [:name]) + end + +end diff --git a/app/views/organizations/registrations/new.html.erb b/app/views/organizations/registrations/new.html.erb new file mode 100644 index 000000000..f8ca107be --- /dev/null +++ b/app/views/organizations/registrations/new.html.erb @@ -0,0 +1,64 @@ +
+
+
+
+

<%= t("devise_views.organizations.registrations.new.title") %>

+ + <%= form_for(resource, as: :user, url: organization_registration_path) do |f| %> + + <%= devise_error_messages! %> + + <%= f.fields_for :organization do |fo| %> +
+
+ <%= fo.label :organization_name, t("devise_views.organizations.registrations.new.organization_name_label") %> + <%= fo.text_field :name, autofocus: true, placeholder: t("devise_views.organizations.registrations.new.organization_name_label") %> +
+
+ <% end %> + +
+
+ <%= f.label :email, t("devise_views.organizations.registrations.new.email_label") %> + <%= f.email_field :email, placeholder: t("devise_views.organizations.registrations.new.email_label") %> +
+
+ +
+
+ <%= f.label :phone_number, t("devise_views.organizations.registrations.new.phone_number_label") %> + <%= f.text_field :phone_number, placeholder: t("devise_views.organizations.registrations.new.phone_number_label") %> +
+
+ +
+
+ <%= f.label :password, t("devise_views.organizations.registrations.new.password_label"), class: "inline-block" %> + <% if @minimum_password_length %> + <%= t("devise_views.organizations.registrations.new.min_length", min: @minimum_password_length) %> + <% end %> + <%= f.password_field :password, autocomplete: "off", placeholder: t("devise_views.organizations.registrations.new.password_label") %> +
+
+ +
+
+ <%= f.label :password_confirmation, t("devise_views.organizations.registrations.new.password_confirmation_label") %> + <%= f.password_field :password_confirmation, autocomplete: "off", placeholder: t("devise_views.organizations.registrations.new.password_confirmation_label") %> +
+
+ + <%= render 'shared/captcha', resource: resource %> + +
+
+ <%= f.submit t("devise_views.organizations.registrations.new.submit"), class: "button radius expand" %> +
+
+ <% end %> + + <%= render "devise/shared/links" %> +
+
+
+
diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 3edd65527..e62df2a65 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -5,6 +5,7 @@ en: debate: Debate user: User vote: Vote + organization: Organization attributes: comment: body: Comment @@ -19,4 +20,6 @@ en: first_name: "First name" last_name: "Last name" nickname: Nickname - password: Password \ No newline at end of file + password: Password + organization: + name: Organization name diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index 6bab3edb6..36af7e1b3 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -5,6 +5,7 @@ es: debate: Debate user: Usuario vote: Voto + organization: Organización attributes: comment: body: Comentario @@ -19,4 +20,6 @@ es: first_name: Nombre last_name: Apellidos nickname: Pseudónimo - password: Contraseña \ No newline at end of file + password: Contraseña + organization: + name: Nombre de organización diff --git a/config/locales/devise_views.en.yml b/config/locales/devise_views.en.yml index 3560ecdc5..a2f2fe1e1 100644 --- a/config/locales/devise_views.en.yml +++ b/config/locales/devise_views.en.yml @@ -60,6 +60,17 @@ en: min_length: "(%{min} characters minimum)" password_confirmation_label: "Confirm password" submit: "Sign up" + organizations: + registrations: + new: + title: "Sign up as organization" + organization_name_label: "Organization name" + email_label: "Email" + password_label: "Password" + phone_number_label: "Phone number" + min_length: "(%{min} characters minimum)" + password_confirmation_label: "Confirm password" + submit: "Sign up" sessions: new: title: "Log in" @@ -77,6 +88,7 @@ en: login: "Log in" signup: "Sign up" signin_with_provider: "Sign in with %{provider}" + organization_signup: "Sign up as an organization" new_password: "Forgot your password?" new_confirmation: "Didn't receive confirmation instructions?" new_unlock: "Didn't receive unlock instructions?" diff --git a/config/locales/devise_views.es.yml b/config/locales/devise_views.es.yml index 50da3f345..0f920ecf2 100644 --- a/config/locales/devise_views.es.yml +++ b/config/locales/devise_views.es.yml @@ -60,6 +60,17 @@ es: min_length: "(mínimo %{min} caracteres)" password_confirmation_label: "Confirmar contraseña" submit: "Registrarse" + organizations: + registrations: + new: + title: "Registrarse como organización" + organization_name_label: "Nombre de la organización" + email_label: "Email" + password_label: "Contraseña" + phone_number_label: "Teléfono" + min_length: "(mínimo %{min} caracteres)" + password_confirmation_label: "Confirmar contraseña" + submit: "Registrarse" sessions: new: title: "Entrar" @@ -76,6 +87,7 @@ es: links: login: "Entrar" signup: "Registrarse" + organization_signup: "Registro para organizaciones" signin_with_provider: "Entrar con %{provider}" new_password: "¿Olvidaste tu contraseña?" new_confirmation: "¿No has recibido instrucciones para confirmar tu cuenta?" diff --git a/config/routes.rb b/config/routes.rb index 286b8237a..ba2fc77c4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,10 @@ Rails.application.routes.draw do devise_for :users, controllers: { registrations: 'users/registrations' } + devise_for :organizations, class_name: 'User', + controllers: { + registrations: 'organizations/registrations', + sessions: 'devise/sessions' + } # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/spec/features/organizations_spec.rb b/spec/features/organizations_spec.rb new file mode 100644 index 000000000..4bee2cef5 --- /dev/null +++ b/spec/features/organizations_spec.rb @@ -0,0 +1,32 @@ +require 'rails_helper' + +feature 'Organizations' do + + scenario 'Organizations can be created' do + user = User.organizations.where(email: 'green@peace.com').first + expect(user).to_not be + + visit new_organization_registration_path + + fill_in 'user_organization_attributes_name', with: 'Greenpeace' + fill_in 'user_email', with: 'green@peace.com' + fill_in 'user_password', with: 'greenpeace' + fill_in 'user_password_confirmation', with: 'greenpeace' + + click_button 'Sign up' + + user = User.organizations.where(email: 'green@peace.com').first + expect(user).to be + expect(user).to be_organization + expect(user.organization).to_not be_verified + end + + scenario "Organization fields are validated" do + visit new_organization_registration_path + click_button 'Sign up' + + expect(page).to have_content "Email can't be blank" + expect(page).to have_content "Password can't be blank" + expect(page).to have_content "Organization name can't be blank" + end +end