Browse Source

User avatar upload at sign up

master
Ilya Konanykhin 8 years ago
parent
commit
a68e7eca56
  1. 1
      Gemfile
  2. 11
      Gemfile.lock
  3. 11
      app/models/user.rb
  4. 6
      app/views/devise/registrations/new.html.haml
  5. 11
      db/migrate/20170118110330_add_attachment_avatar_to_users.rb
  6. 6
      db/schema.rb

1
Gemfile

@ -18,6 +18,7 @@ gem 'jquery-rails', '~> 2.0'
gem 'pg', '~> 0.17.1' gem 'pg', '~> 0.17.1'
gem 'will_paginate', '~> 3.0.3' gem 'will_paginate', '~> 3.0.3'
gem 'jbuilder', '~> 2.0.3' gem 'jbuilder', '~> 2.0.3'
gem 'paperclip', '~> 4.3'
# Assets # Assets
gem 'sass-rails', '~> 3.0' gem 'sass-rails', '~> 3.0'

11
Gemfile.lock

@ -57,7 +57,10 @@ GEM
xpath (~> 2.0) xpath (~> 2.0)
childprocess (0.5.9) childprocess (0.5.9)
ffi (~> 1.0, >= 1.0.11) ffi (~> 1.0, >= 1.0.11)
climate_control (0.1.0)
cliver (0.3.2) cliver (0.3.2)
cocaine (0.5.8)
climate_control (>= 0.0.3, < 1.0)
coderay (1.1.1) coderay (1.1.1)
coffee-rails (3.2.2) coffee-rails (3.2.2)
coffee-script (>= 2.2.0) coffee-script (>= 2.2.0)
@ -129,6 +132,7 @@ GEM
treetop (~> 1.4.8) treetop (~> 1.4.8)
method_source (0.8.2) method_source (0.8.2)
mime-types (1.25.1) mime-types (1.25.1)
mimemagic (0.3.0)
mini_portile2 (2.0.0) mini_portile2 (2.0.0)
momentjs-rails (2.15.1) momentjs-rails (2.15.1)
railties (>= 3.1) railties (>= 3.1)
@ -148,6 +152,12 @@ GEM
nenv (~> 0.1) nenv (~> 0.1)
shellany (~> 0.0) shellany (~> 0.0)
orm_adapter (0.0.7) orm_adapter (0.0.7)
paperclip (4.3.7)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
cocaine (~> 0.5.5)
mime-types
mimemagic (= 0.3.0)
pg (0.17.1) pg (0.17.1)
poltergeist (1.5.1) poltergeist (1.5.1)
capybara (~> 2.1) capybara (~> 2.1)
@ -261,6 +271,7 @@ DEPENDENCIES
netzke-basepack (~> 0.8.0) netzke-basepack (~> 0.8.0)
netzke-cancan netzke-cancan
netzke-core (~> 0.8.0) netzke-core (~> 0.8.0)
paperclip (~> 4.3)
pg (~> 0.17.1) pg (~> 0.17.1)
poltergeist (~> 1.5.0) poltergeist (~> 1.5.0)
pry (~> 0.9.8) pry (~> 0.9.8)

11
app/models/user.rb

@ -1,4 +1,6 @@
class User < ActiveRecord::Base class User < ActiveRecord::Base
MAX_AVATAR_SIZE_KB = 1024
acts_as_loggable acts_as_loggable
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
@ -8,7 +10,7 @@ class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model # Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, attr_accessible :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :bike_id, :first_name, :last_name, :bike_id,
:user_profiles_attributes, :username :user_profiles_attributes, :username, :avatar
has_many :transactions, as: :customer has_many :transactions, as: :customer
has_many :transaction_logs, through: :transactions, source: :logs has_many :transaction_logs, through: :transactions, source: :logs
@ -20,11 +22,18 @@ class User < ActiveRecord::Base
belongs_to :bike belongs_to :bike
has_attached_file :avatar, :styles => {:thumb => '100x100>'}
default_scope order('username ASC') default_scope order('username ASC')
validates :first_name, :presence => true validates :first_name, :presence => true
validates :last_name, :presence => true validates :last_name, :presence => true
validates_attachment :avatar, :content_type => {:content_type => %w{ image/jpeg image/gif image/png }},
:file_name => {:matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]},
:size => {:in => 0..MAX_AVATAR_SIZE_KB.kilobytes}
def to_s def to_s
"#{first_name} #{last_name}" "#{first_name} #{last_name}"
end end

6
app/views/devise/registrations/new.html.haml

@ -23,6 +23,12 @@
= f.fields_for :user_profiles, profile_builder do |builder| = f.fields_for :user_profiles, profile_builder do |builder|
= render 'user_profile_fields', f: builder = render 'user_profile_fields', f: builder
%fieldset
.form-group
%label You may upload your photo to be used as an avatar
= f.file_field :avatar
.help-block #{User::MAX_AVATAR_SIZE_KB} Kb max
%fieldset %fieldset
.form-group .form-group
= f.password_field :password, placeholder: 'Password', class: 'form-control' = f.password_field :password, placeholder: 'Password', class: 'form-control'

11
db/migrate/20170118110330_add_attachment_avatar_to_users.rb

@ -0,0 +1,11 @@
class AddAttachmentAvatarToUsers < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.attachment :avatar
end
end
def self.down
remove_attachment :users, :avatar
end
end

6
db/schema.rb

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20131019170248) do ActiveRecord::Schema.define(:version => 20170118110330) do
create_table "bike_actions", :force => true do |t| create_table "bike_actions", :force => true do |t|
t.string "action", :limit => 128, :null => false t.string "action", :limit => 128, :null => false
@ -198,6 +198,10 @@ ActiveRecord::Schema.define(:version => 20131019170248) do
t.string "first_name", :default => "", :null => false t.string "first_name", :default => "", :null => false
t.string "last_name", :default => "", :null => false t.string "last_name", :default => "", :null => false
t.string "username" t.string "username"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
end end
add_index "users", ["bike_id"], :name => "index_users_on_bike_id", :unique => true add_index "users", ["bike_id"], :name => "index_users_on_bike_id", :unique => true

Loading…
Cancel
Save