Skip to content

Commit

Permalink
add backend support for profile pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Spies authored and Greg Spies committed Oct 20, 2016
1 parent 0303944 commit b964279
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 139 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gem 'gravatarify', '~> 3.0.0'
gem 'slodown', '~> 0.1.3'
gem 'scenic', '~> 1.2.0'
gem 'awesome_nested_set', '~> 3.1.1'
gem 'paperclip', "~> 5.0.0"

gem 'omniauth', '~> 1.2.2'
gem 'omniauth-facebook', '~> 1.6.0'
Expand Down
14 changes: 13 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
climate_control (0.0.3)
activesupport (>= 3.0)
cocaine (0.5.8)
climate_control (>= 0.0.3, < 1.0)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
Expand Down Expand Up @@ -158,6 +162,7 @@ GEM
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (2.99.2)
mimemagic (0.3.2)
mini_portile2 (2.1.0)
minitest (5.9.0)
multi_json (1.10.1)
Expand Down Expand Up @@ -193,6 +198,12 @@ GEM
omniauth-orcid (0.6)
omniauth (~> 1.0)
omniauth-oauth2 (~> 1.1)
paperclip (5.0.0)
activemodel (>= 4.2.0)
activesupport (>= 4.2.0)
cocaine (~> 0.5.5)
mime-types
mimemagic (~> 0.3.0)
pg (0.18.2)
pkg-config (1.1.7)
polyamorous (1.2.0)
Expand Down Expand Up @@ -363,6 +374,7 @@ DEPENDENCIES
omniauth-github (~> 1.1.2)
omniauth-google-oauth2 (~> 0.2.5)
omniauth-orcid (~> 0.6.0)
paperclip (~> 5.0.0)
pg (~> 0.18.2)
pry
pry-nav
Expand All @@ -381,4 +393,4 @@ DEPENDENCIES
yajl-ruby (~> 1.2.1)

BUNDLED WITH
1.11.2
1.12.5
3 changes: 2 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def events
def update
user = get_user
authorize user
binding.pry
status = if user.update_attributes(user_attributes)
:ok
else
Expand Down Expand Up @@ -72,6 +73,6 @@ def get_user
end

def user_attributes
params.permit(:email, :url, :username, :name, :area_of_expertise, :orcid, :twitter_handle, :facebook_profile, :linkedin_profile, :bio, :signup_complete, :accepted_license)
params.permit(:email, :url, :username, :name, :area_of_expertise, :orcid, :twitter_handle, :facebook_profile, :linkedin_profile, :bio, :signup_complete, :accepted_license, :profile_pic_file_name, :profile_pic_content_type, :profile_pic_file_size, :profile_pic_updated_at)
end
end
24 changes: 24 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ class User < ActiveRecord::Base
validate :username_is_not_role_name
after_create :assign_default_username
after_save :check_for_signup_completion
after_save :check_for_pic_upload
has_attached_file :profile_pic,
:styles => {
"x128" => "128x128",
"x64" => "64x64",
"x32" => "32x32",
"x14" => "14x14"},
:url => "/profile-images/:style/:basename.:extension",
:path => ":rails_root/public/profile-images/:style/:basename.:extension",
:default_url => "/profile-images/:style/default.png",
if: :profile_pic.present?
validates_attachment_content_type :profile_pic, content_type: /\Aimage/,
unless: :profile_pic_content_type
validates_attachment_size :profile_pic, less_than: 2.megabytes,
unless: :profile_pic_file_size
validates_attachment_file_name :profile_pic, matches: [/png\z/, /jpe?g\z/],
unless: :profile_pic_file_name

def self.datatable_scope
joins('LEFT OUTER JOIN events ON events.originating_user_id = users.id')
Expand Down Expand Up @@ -144,4 +161,11 @@ def check_for_signup_completion
self.save
end
end

def check_for_pic_upload
if self.errors.none? && self.profile_pic.present?
self.uploaded_pic = true
self.save
end
end
end
7 changes: 6 additions & 1 deletion app/presenters/user_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ def as_json(options = {})
bio: user.bio,
featured_expert: user.featured_expert,
accepted_license: user.accepted_license,
signup_complete: user.signup_complete
signup_complete: user.signup_complete,
profile_pic_file_name: user.profile_pic_file_name,
profile_pic_content_type: user.profile_pic_content_type,
profile_pic_file_size: user.profile_pic_file_size,
profile_pic_updated_at: user.profile_pic_updated_at,
uploaded_pic: user.uploaded_pic
}.merge(additional_attributes)
end
end
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20161017192146_add_profile_pic_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddProfilePicToUsers < ActiveRecord::Migration
def change
add_attachment :users, :profile_pic
add_column :users, :uploaded_pic, :boolean, default: false
end
end

0 comments on commit b964279

Please sign in to comment.