From 969c24c091b1d852127ee41850584a25066cf073 Mon Sep 17 00:00:00 2001 From: Stella Rouzi Date: Sat, 18 Apr 2015 21:44:40 +0300 Subject: [PATCH] Move common views to partial --- .../conference_registrations_controller.rb | 23 +++++++++---- app/helpers/application_helper.rb | 8 +++++ app/models/registration.rb | 4 +-- .../conference_registrations/_form.html.haml | 32 ++---------------- app/views/devise/registrations/edit.html.haml | 2 +- app/views/devise/registrations/new.html.haml | 21 ++---------- app/views/devise/sessions/new.html.haml | 20 ++--------- app/views/devise/shared/_help.html.haml | 5 +++ app/views/devise/shared/_links.html.haml | 2 +- app/views/devise/shared/_openid.html.haml | 20 +++++++---- .../devise/shared/_openid_links.html.haml | 7 ++++ .../shared/_sign_in_form_embedded.html.haml | 25 ++++++++++++++ .../devise/shared/_sign_up_fields.html.haml | 4 --- .../shared/_sign_up_form_embedded.html.haml | 8 +++++ app/views/layouts/_navigation.html.haml | 7 ++-- app/views/proposal/new.html.haml | 33 ++----------------- 16 files changed, 101 insertions(+), 120 deletions(-) create mode 100644 app/views/devise/shared/_help.html.haml create mode 100644 app/views/devise/shared/_openid_links.html.haml create mode 100644 app/views/devise/shared/_sign_in_form_embedded.html.haml delete mode 100644 app/views/devise/shared/_sign_up_fields.html.haml create mode 100644 app/views/devise/shared/_sign_up_form_embedded.html.haml diff --git a/app/controllers/conference_registrations_controller.rb b/app/controllers/conference_registrations_controller.rb index 2a48e52a..7b1182e9 100644 --- a/app/controllers/conference_registrations_controller.rb +++ b/app/controllers/conference_registrations_controller.rb @@ -14,8 +14,14 @@ class ConferenceRegistrationsController < ApplicationController redirect_to root_path, alert: 'You need to sign in or sign up before continuing.' return end + + # avoid openid sign_in to redirect to register/new when the sign_in user had already a registration + if current_user && @conference.user_registered?(current_user) + redirect_to edit_conference_conference_registrations_path(@conference.short_title) + end + @registration = Registration.new - @registration.build_user + @user = @registration.build_user end def show @@ -27,9 +33,8 @@ class ConferenceRegistrationsController < ApplicationController def edit; end def create - @registration = Registration.new(registration_params) - @registration.conference = @conference - @registration.user = current_user if current_user + @registration = @conference.registrations.new(registration_params) + @registration.user = current_user || ( @user = @registration.build_user(user_params) ) if @registration.save # Trigger ahoy event @@ -47,7 +52,7 @@ class ConferenceRegistrationsController < ApplicationController redirect_to conference_conference_registrations_path(@conference.short_title) end else - flash[:error] = "An error prohibited the registration for #{@conference.title}: "\ + flash[:error] = "Could not create your registration for #{@conference.title}: "\ "#{@registration.errors.full_messages.join('. ')}." render :new end @@ -58,7 +63,7 @@ class ConferenceRegistrationsController < ApplicationController redirect_to conference_conference_registrations_path(@conference.short_title), notice: 'Registration was successfully updated.' else - flash[:error] = "An error prohibited the registration for #{@conference.title}: "\ + flash[:error] = "Could not update your registration for #{@conference.title}: "\ "#{@registration.errors.full_messages.join('. ')}." render :edit end @@ -70,7 +75,7 @@ class ConferenceRegistrationsController < ApplicationController notice: "You are not registered for #{@conference.title} anymore!" else redirect_to root_path, - error: "An error prohibited deleting the registration for #{@conference.title}: "\ + error: "Could not update your registration for #{@conference.title}: "\ "#{@registration.errors.full_messages.join('. ')}." end end @@ -85,6 +90,10 @@ class ConferenceRegistrationsController < ApplicationController end end + def user_params + params.require(:user).permit(:username, :email, :name, :password, :password_confirmation) + end + def registration_params params.require(:registration). permit( diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8fb9c7ae..3c22da91 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,12 @@ module ApplicationHelper + def resource_name + :user + end + + def devise_mapping + @devise_mapping ||= Devise.mappings[:user] + end + def pluralize_without_count(count, noun, text = nil) if count != 0 count == 1 ? "#{noun}#{text}" : "#{noun.pluralize}#{text}" diff --git a/app/models/registration.rb b/app/models/registration.rb index e2bc78c4..42e0be19 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -1,7 +1,5 @@ class Registration < ActiveRecord::Base belongs_to :user - validates :user, presence: true - accepts_nested_attributes_for :user belongs_to :conference belongs_to :dietary_choice @@ -29,6 +27,8 @@ class Registration < ActiveRecord::Base alias_attribute :other_needs, :other_special_needs + validates :user, presence: true + validates_uniqueness_of :user_id, scope: :conference_id, message: 'already Registered!' after_create :set_week, :subscribe_to_conference, :send_registration_mail diff --git a/app/views/conference_registrations/_form.html.haml b/app/views/conference_registrations/_form.html.haml index 073eae1d..f3b282f1 100644 --- a/app/views/conference_registrations/_form.html.haml +++ b/app/views/conference_registrations/_form.html.haml @@ -19,9 +19,8 @@ .tab-content .tab-pane.active{role: 'tabpanel', id: 'signup'} = semantic_form_for(@registration, url: conference_conference_registrations_path(@conference.short_title)) do |f| - - if !current_user - = f.fields_for :user do |u| - = render partial: 'devise/shared/sign_up_fields', locals: { u: u} + = render partial: 'devise/shared/sign_up_form_embedded' + - if @conference.questions.any? = render partial: 'questions', locals: { f: f } - if @conference.events.workshops.any? @@ -38,29 +37,4 @@ - else =f.action :submit, button_html: { value: 'Register', class: 'btn btn-primary', id: 'register' } .tab-pane{role: 'tabpanel', id: 'signin'} - - if !CONFIG['authentication']['ichain']['enabled'] - = form_tag(new_user_session_path, class: 'form-horizontal') do - %legend - %span - Sign In - .form-group - %label{for: "user[login]", class: 'col-sm-2 control-label'} - Username - .col-sm-10 - = text_field_tag 'user[login]', nil, placeholder: 'Username', class: 'form-control', required: 'required' - .form-group - %label{for: "user[password]", class: 'col-sm-2 control-label'} - Password - .col-sm-10 - = password_field_tag 'user[password]', nil, placeholder: 'Password', class: 'form-control', required: 'required' - .form-group - .col-sm-12 - %button.btn.btn-success.pull-right - Sign in - - unless omniauth_configured.empty? - .form-group - %hr - %p.text-center - or sign in using - .text-center - = render 'devise/shared/openid' + = render partial: 'devise/shared/sign_in_form_embedded' diff --git a/app/views/devise/registrations/edit.html.haml b/app/views/devise/registrations/edit.html.haml index 756e00cf..964ec334 100644 --- a/app/views/devise/registrations/edit.html.haml +++ b/app/views/devise/registrations/edit.html.haml @@ -21,7 +21,7 @@ openID while logged in to OSEM - if User.omniauth_providers.present? #openidlinks - = render 'devise/shared/openid' + = render 'devise/shared/openid_links' = f.inputs name: 'Confirmation' do = f.input :current_password, input_html: {autocomplete: 'off'}, hint: '(we need your current password to confirm password, email or username changes)' diff --git a/app/views/devise/registrations/new.html.haml b/app/views/devise/registrations/new.html.haml index f866ae26..8a1afa20 100644 --- a/app/views/devise/registrations/new.html.haml +++ b/app/views/devise/registrations/new.html.haml @@ -10,21 +10,6 @@ = render partial: 'devise/shared/sign_up_fields', locals: { u: f} %p.text-right = f.action :submit, as: :button, label: 'Sign Up', button_html: {class: 'btn btn-success' } - - unless omniauth_configured.empty? - .row - .col-md-4 - %hr - .col-md-4 - %h4.text-center - or sign up using - .col-md-4 - %hr - .row - .col-md-12 - #openidlinks - = render 'devise/shared/openid' - %p.text-right - %a.small{"data-toggle" => "collapse", "data-target" => "#devise-help"} - Need Help? - #devise-help.collapse - = render 'devise/shared/links' + + = render partial: 'devise/shared/openid' + = render partial: 'devise/shared/help' diff --git a/app/views/devise/sessions/new.html.haml b/app/views/devise/sessions/new.html.haml index 8988d9f8..1ef5c169 100644 --- a/app/views/devise/sessions/new.html.haml +++ b/app/views/devise/sessions/new.html.haml @@ -14,21 +14,5 @@ = f.check_box :remember_me %p.text-right = f.action :submit, as: :button, label: 'Sign In', button_html: {class: 'btn btn-success'} - - unless omniauth_configured.empty? - .row - .col-md-4 - %hr - .col-md-4 - %h4.text-center - or sign in using - .col-md-4 - %hr - .row - .col-md-12 - #openidlinks - = render 'devise/shared/openid' - %p.text-right - %a.small{"data-toggle" => "collapse", "data-target" => "#devise-help"} - Need Help? - #devise-help.collapse - = render 'devise/shared/links' + = render partial: 'devise/shared/openid' + = render partial: 'devise/shared/help' diff --git a/app/views/devise/shared/_help.html.haml b/app/views/devise/shared/_help.html.haml new file mode 100644 index 00000000..50962d6d --- /dev/null +++ b/app/views/devise/shared/_help.html.haml @@ -0,0 +1,5 @@ +%p.text-right + %a.small.btn.btn-default{"data-toggle" => "collapse", "data-target" => "#devise-help"} + Need Help? +#devise-help.collapse + = render 'devise/shared/links' diff --git a/app/views/devise/shared/_links.html.haml b/app/views/devise/shared/_links.html.haml index b320965b..568259ca 100644 --- a/app/views/devise/shared/_links.html.haml +++ b/app/views/devise/shared/_links.html.haml @@ -2,7 +2,7 @@ = link_to "Forgot your password?", new_password_path(resource_name) %br -- if devise_mapping.confirmable? && controller_name != 'confirmations' && controller_name == 'passwords' +- if devise_mapping.confirmable? && controller_name != 'confirmations' = link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %br diff --git a/app/views/devise/shared/_openid.html.haml b/app/views/devise/shared/_openid.html.haml index 64f5f72a..f850e4cc 100644 --- a/app/views/devise/shared/_openid.html.haml +++ b/app/views/devise/shared/_openid.html.haml @@ -1,7 +1,13 @@ -.btn-group.btn-group-lg - - omniauth_configured.each do |provider| - = link_to user_omniauth_authorize_path(provider), - class: "btn btn-success btn-lg", - id: "omniauth-#{provider}", - title: "Your #{provider} login" do - %i{:class => "fa fa-#{provider}"} \ No newline at end of file +- unless omniauth_configured.empty? + .row + .col-md-4 + %hr + .col-md-4 + %h4.text-center + or sign in using + .col-md-4 + %hr + .row + .col-md-12 + #openidlinks + = render partial: 'devise/shared/openid_links' diff --git a/app/views/devise/shared/_openid_links.html.haml b/app/views/devise/shared/_openid_links.html.haml new file mode 100644 index 00000000..463d3410 --- /dev/null +++ b/app/views/devise/shared/_openid_links.html.haml @@ -0,0 +1,7 @@ +.text-center + .btn-group.btn-group-lg + - omniauth_configured.each do |provider| + = link_to user_omniauth_authorize_path(provider), class: "btn btn-success btn-lg", + id: "omniauth-#{provider}", + title: "Your #{provider} login" do + %i{:class => "fa fa-#{provider}"} diff --git a/app/views/devise/shared/_sign_in_form_embedded.html.haml b/app/views/devise/shared/_sign_in_form_embedded.html.haml new file mode 100644 index 00000000..43704315 --- /dev/null +++ b/app/views/devise/shared/_sign_in_form_embedded.html.haml @@ -0,0 +1,25 @@ +- if !CONFIG['authentication']['ichain']['enabled'] + = form_tag(new_user_session_path, class: 'form-horizontal') do + %legend + %span + Sign In + .form-group + %label{for: "user[login]", class: 'col-sm-2 control-label'} + Username + .col-sm-10 + = text_field_tag 'user[login]', nil, placeholder: 'Username', class: 'form-control', required: 'required' + .form-group + %label{for: "user[password]", class: 'col-sm-2 control-label'} + Password + .col-sm-10 + = password_field_tag 'user[password]', nil, placeholder: 'Password', class: 'form-control', required: 'required' + .form-group + .col-sm-12 + %button.btn.btn-success.pull-right + Sign in + = render partial: 'devise/shared/openid' + %p.text-right + %a.small.btn.btn-default{"data-toggle" => "collapse", "data-target" => "#devise-help-sign-in"} + Need Help? + #devise-help-sign-in.collapse + = render partial: 'devise/shared/links' diff --git a/app/views/devise/shared/_sign_up_fields.html.haml b/app/views/devise/shared/_sign_up_fields.html.haml deleted file mode 100644 index 9cced15f..00000000 --- a/app/views/devise/shared/_sign_up_fields.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -= u.input :username, input_html: {required: 'required', autocomplete: 'off'} -= u.input :email, input_html: {required: 'required', autocomplete: 'off'} -= u.input :password, input_html: {required: 'required', autocomplete: 'off', id: 'password_inline'} -= u.input :password_confirmation, :required => true, input_html: {required: 'required', autocomplete: 'off'} diff --git a/app/views/devise/shared/_sign_up_form_embedded.html.haml b/app/views/devise/shared/_sign_up_form_embedded.html.haml new file mode 100644 index 00000000..5bc038f0 --- /dev/null +++ b/app/views/devise/shared/_sign_up_form_embedded.html.haml @@ -0,0 +1,8 @@ +- unless current_user + = semantic_fields_for @user do |u| + = u.input :username, input_html: { required: 'required', autocomplete: 'off' } + = u.input :email, input_html: { required: 'required', autocomplete: 'off' } + = u.input :password, input_html: { required: 'required', autocomplete: 'off', id: 'password_inline' } + = u.input :password_confirmation, input_html: { required: 'required', autocomplete: 'off' } + = render partial: 'devise/shared/openid' + = render partial: 'devise/shared/help' diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index ff0a5698..ea837d2c 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -60,11 +60,12 @@ .divider %h6.text-center or - = render 'devise/shared/openid' + = render 'devise/shared/openid_links' %p.text-right - %a.small{"data-toggle" => "collapse", "data-target" => "#navbar-devise-help"} + %br + %a.small.btn.btn-xs.btn-default{"data-toggle" => "collapse", "data-target" => "#navbar-devise-help"} Need Help? #navbar-devise-help.collapse - = link_to "Forgot your password?", new_password_path(User.new) + = render 'devise/shared/links' %li.hidden-lg = link_to('Sign In', new_user_session_path) diff --git a/app/views/proposal/new.html.haml b/app/views/proposal/new.html.haml index 03db0032..1d91c157 100644 --- a/app/views/proposal/new.html.haml +++ b/app/views/proposal/new.html.haml @@ -22,9 +22,8 @@ = semantic_form_for(@event, url: @url) do |f| - - unless current_user - = semantic_fields_for @user do |u| - = render partial: 'devise/shared/sign_up_fields', locals: { u: u } + = render partial: 'devise/shared/sign_up_form_embedded' + = f.inputs name: 'Proposal Information' do = f.input :title, as: :string, required: true = f.input :event_type_id, as: :select, @@ -64,30 +63,4 @@ %p.text-right = f.action :submit, :as => :button, :button_html => {:class => "btn btn-success"}, label: 'Create Proposal' .tab-pane{role: 'tabpanel', id: 'signin'} - - if !CONFIG['authentication']['ichain']['enabled'] - = form_tag(new_user_session_path, class: 'form-horizontal') do - %legend - %span - Sign In - .form-group - %label{for: "user[login]", class: 'col-sm-2 control-label'} - Username - .col-sm-10 - = text_field_tag 'user[login]', nil, placeholder: 'Username', class: 'form-control', required: 'required' - .form-group - %label{for: "user[password]", class: 'col-sm-2 control-label'} - Password - .col-sm-10 - = password_field_tag 'user[password]', nil, placeholder: 'Password', class: 'form-control', required: 'required' - .form-group - .col-sm-12 - %button.btn.btn-success.pull-right - Sign in - - unless omniauth_configured.empty? - .form-group - %hr - %p.text-center - or sign in using - .text-center - = render 'devise/shared/openid' - + = render partial: 'devise/shared/sign_in_form_embedded'