From 5554ffd6aabdd02f6d6f68fc265ca91fbce5eeb1 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Sun, 31 Aug 2014 16:02:56 +0300 Subject: [PATCH] Picture uploading --- Gemfile | 3 ++ Gemfile.lock | 6 +++ app/controllers/application_controller.rb | 2 +- app/controllers/sessions_controller.rb | 9 ++++ app/models/speaker_profile.rb | 4 +- app/uploaders/picture_uploader.rb | 51 +++++++++++++++++++ app/views/devise/registrations/edit.html.slim | 31 ++++++----- config/locales/bg.yml | 2 + config/locales/simple_form.bg.yml | 1 + config/routes.rb | 2 +- ...ture_url_to_picture_in_speaker_profiles.rb | 5 ++ public/uploads/.keep | 0 public/uploads/tmp/.keep | 0 13 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/uploaders/picture_uploader.rb create mode 100644 db/migrate/20140831121527_rename_picture_url_to_picture_in_speaker_profiles.rb create mode 100644 public/uploads/.keep create mode 100644 public/uploads/tmp/.keep diff --git a/Gemfile b/Gemfile index d09fcca..9e8c2c8 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,9 @@ gem 'simple_form' # Phone validation gem 'phony_rails' +# Picture uploads +gem 'carrierwave' + group :development do gem 'spring' gem 'spring-commands-rspec' diff --git a/Gemfile.lock b/Gemfile.lock index 2c5503c..06dfa18 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,6 +36,11 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) + carrierwave (0.10.0) + activemodel (>= 3.2.0) + activesupport (>= 3.2.0) + json (>= 1.7) + mime-types (>= 1.16) celluloid (0.15.2) timers (~> 1.1.0) choice (0.1.6) @@ -221,6 +226,7 @@ PLATFORMS DEPENDENCIES capybara + carrierwave coffee-rails devise devise-i18n diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1e5eaf1..948c310 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,7 +8,7 @@ class ApplicationController < ActionController::Base def configure_permitted_parameters devise_parameter_sanitizer.for(:account_update) do |u| - u.permit(:email, :password, :password_confirmation, :current_password, speaker_profile_attributes: [:first_name, :last_name, :public_email, :organisation, :github, :twitter, :mobile_phone, :biography]) + u.permit(:email, :password, :password_confirmation, :current_password, speaker_profile_attributes: [:first_name, :last_name, :public_email, :organisation, :github, :twitter, :mobile_phone, :biography, :picture, :id]) end end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..c9f6a1a --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,9 @@ +class SessionsController < Devise::SessionsController + def after_sign_in_path_for(user) + if user.speaker_profile.present? + stored_location_for(user) || signed_in_root_path(user) + else + edit_user_registration_path + end + end +end diff --git a/app/models/speaker_profile.rb b/app/models/speaker_profile.rb index 4af25d1..9e0ce34 100644 --- a/app/models/speaker_profile.rb +++ b/app/models/speaker_profile.rb @@ -3,7 +3,7 @@ class SpeakerProfile < ActiveRecord::Base validates :first_name, presence: true validates :last_name, presence: true - validates :photo_url, presence: true + validates :picture, presence: true validates :mobile_phone, phony_plausible: true, presence: true validates :biography, presence: true validates :public_email, format: {with: /\A[^@]+@[^@]+\z/}, allow_blank: true @@ -12,6 +12,8 @@ class SpeakerProfile < ActiveRecord::Base phony_normalize :mobile_phone, default_country_code: 'BG' + mount_uploader :picture, PictureUploader + def twitter=(handle) write_attribute :twitter, handle.gsub(/\A@/,'') if handle end diff --git a/app/uploaders/picture_uploader.rb b/app/uploaders/picture_uploader.rb new file mode 100644 index 0000000..217f662 --- /dev/null +++ b/app/uploaders/picture_uploader.rb @@ -0,0 +1,51 @@ +# encoding: utf-8 + +class PictureUploader < CarrierWave::Uploader::Base + + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + # include CarrierWave::MiniMagick + + # Choose what kind of storage to use for this uploader: + storage :file + # storage :fog + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + # def default_url + # # For Rails 3.1+ asset pipeline compatibility: + # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) + # + # "/images/fallback/" + [version_name, "default.png"].compact.join('_') + # end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + # version :thumb do + # process :resize_to_fit => [50, 50] + # end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + # def extension_white_list + # %w(jpg jpeg gif png) + # end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + +end diff --git a/app/views/devise/registrations/edit.html.slim b/app/views/devise/registrations/edit.html.slim index 205756f..53d5b36 100644 --- a/app/views/devise/registrations/edit.html.slim +++ b/app/views/devise/registrations/edit.html.slim @@ -1,22 +1,9 @@ -h2.entry-title Редакция на акаунт - = simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| - = f.error_notification - - .form-inputs - h3 Данни за вход в системата - = f.input :email, required: true, autofocus: true - - - if devise_mapping.confirmable? && resource.pending_reconfirmation? - p Currently waiting confirmation for: #{resource.unconfirmed_email} - - = f.input :password, autocomplete: "off", hint: "leave it blank if you don't want to change it", required: false - = f.input :password_confirmation, required: false - = f.input :current_password, hint: "we need your current password to confirm your changes", required: true - .form_inputs - h3 Лекторски профил + h2.entry-title Лекторски профил + = f.error_notification = f.simple_fields_for :speaker_profile do |ff| + = ff.input :picture, as: :file = ff.input :first_name = ff.input :last_name = ff.input :public_email @@ -26,5 +13,17 @@ h2.entry-title Редакция на акаунт = ff.input :mobile_phone, input_html: {value: resource.speaker_profile.mobile_phone.try(:phony_formatted, format: :international)} = ff.input :biography + .form-inputs + h3.entry-title Данни за вход в системата + = f.input :email, required: true, autofocus: true + + - if devise_mapping.confirmable? && resource.pending_reconfirmation? + p + Очаква се потвърждение на: #{resource.unconfirmed_email} + + = f.input :password, autocomplete: "off", hint: "Не попълвайте, ако не желаете да промените паролата си", required: false + = f.input :password_confirmation, required: false + = f.input :current_password, hint: "Попълнете, ако искате да промените паролата или имейл адреса си.", required: true + .form-actions = f.button :submit, 'Обнови' diff --git a/config/locales/bg.yml b/config/locales/bg.yml index 1a0bf49..91c3d13 100644 --- a/config/locales/bg.yml +++ b/config/locales/bg.yml @@ -35,10 +35,12 @@ bg: attributes: user: email: E-mail + current_password: Текуща парола password: Парола password_confirmation: Отново паролата remember_me: Запомни ме speaker_profile: + picture: Снимка first_name: Име last_name: Фамилия organisation: Организация diff --git a/config/locales/simple_form.bg.yml b/config/locales/simple_form.bg.yml index 4adc6e2..c9a6497 100644 --- a/config/locales/simple_form.bg.yml +++ b/config/locales/simple_form.bg.yml @@ -13,6 +13,7 @@ bg: password: Парола с дължина между 8 и 128 символа password_confirmation: Отново въведената отгоре парола speaker_profile: + picture: Ваша снимка first_name: Малкото Ви име last_name: Фамилното Ви име organisation: Организацията, която представлявате diff --git a/config/routes.rb b/config/routes.rb index 0b5afc5..13994b4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ Rails.application.routes.draw do resources :lectures, only: [:index, :new, :create, :edit, :update, :show] resources :workshops, only: [:index, :new, :create, :edit, :update, :show] - devise_for :users, controllers: {registrations: 'registrations'} + devise_for :users, controllers: {registrations: 'registrations', sessions: 'sessions'} resource :user, only: [] do resource :speaker_profile, only: [:edit, :update] diff --git a/db/migrate/20140831121527_rename_picture_url_to_picture_in_speaker_profiles.rb b/db/migrate/20140831121527_rename_picture_url_to_picture_in_speaker_profiles.rb new file mode 100644 index 0000000..fc253c3 --- /dev/null +++ b/db/migrate/20140831121527_rename_picture_url_to_picture_in_speaker_profiles.rb @@ -0,0 +1,5 @@ +class RenamePictureUrlToPictureInSpeakerProfiles < ActiveRecord::Migration + def change + rename_column :speaker_profiles, :photo_url, :picture + end +end diff --git a/public/uploads/.keep b/public/uploads/.keep new file mode 100644 index 0000000..e69de29 diff --git a/public/uploads/tmp/.keep b/public/uploads/tmp/.keep new file mode 100644 index 0000000..e69de29