Skip to content

Commit

Permalink
Refactoring profile and user to make them user a resource for avatar …
Browse files Browse the repository at this point in the history
…instead of giving links to the picture.

Makes much more sense this way even though I have to add a field in the database which I tried to avoid. But too much serialized options can sometimes be too much.

Also add specs because that controller lacked love.

Issues publify#230 and publify#231.
  • Loading branch information
Frédéric de Villamil committed Sep 12, 2013
1 parent 79fa967 commit 52c4b33
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 118 deletions.
6 changes: 1 addition & 5 deletions app/controllers/admin/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ def index
@user.attributes = params[:user]
if request.post?
if params[:user][:filename]
avatar = upload_avatar
@user.avatar = avatar.upload.avatar.url
@user.thumb_avatar = avatar.upload.thumb.url
@user.medium_avatar = avatar.upload.medium.url
@user.large_avatar = avatar.upload.url
@user.resource = upload_avatar
end

if @user.save
Expand Down
15 changes: 6 additions & 9 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,18 @@ def html(content, what = :all, deprecated = false)
content.html(what)
end

def display_user_avatar(user_id, size='avatar', klass='alignleft')
user = User.find(user_id)

if user.avatar.present?
def display_user_avatar(user, size='avatar', klass='alignleft')
if user.resource.present?
avatar = case size
when 'thumb'
user.thumb_avatar
user.resource.upload.thumb.url
when 'medium'
user.medium_avatar
user.resource.upload.medium.url
when 'large'
user.large_avatar
user.resource.upload.large.url
else
user.avatar
user.resource.upload.avatar.url
end

elsif user.twitter_profile_image.present?
avatar = user.twitter_profile_image.present?
end
Expand Down
6 changes: 6 additions & 0 deletions app/helpers/authors_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def is_url?(str)
end
end

def author_description(user)
return unless user.description.present?

content_tag(:div, user.description, id: 'author-description')
end

def author_link(article)
return h(article.author) if just_author?(article.user)
return h(article.user.name) if just_name?(article.user)
Expand Down
7 changes: 2 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class User < ActiveRecord::Base

belongs_to :profile
belongs_to :text_filter

belongs_to :resource

delegate :name, :to => :text_filter, :prefix => true
delegate :label, :to => :profile, :prefix => true

Expand Down Expand Up @@ -38,10 +39,6 @@ class User < ActiveRecord::Base
setting :twitter_oauth_token, :string, ''
setting :twitter_oauth_token_secret, :string, ''
setting :twitter_profile_image, :string, ''
setting :avatar, :string, ''
setting :thumb_avatar, :string, ''
setting :medium_avatar, :string, ''
setting :large_avatar, :string, ''

# echo "publify" | sha1sum -
class_attribute :salt
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<% unless controller.controller_name == 'users'%>
<fieldset class='form-horizontal'>
<legend><%= _("Avatar") %></legend>
<p><%= display_user_avatar(current_user.id) %> <%= _("Your current avatar") %></p>
<p><%= display_user_avatar(current_user, 'thumb') %> <%= _("Your current avatar") %></p>
<div class='control-group'>
<label class='control-label' for="avatar"><%= _("Upload your image")%></label>
<div class='controls'>
Expand Down
5 changes: 3 additions & 2 deletions app/views/authors/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<div class="post">
<h2><%= @author.nickname %></h2>
<%= display_user_avatar(@author.id, 'thumb', 'alignright') %>
<%= @author.description%>
<%= display_user_avatar(@author, 'thumb', 'alignright') %>
<%= author_description @author %>

<h2><%= _("Contact information") %></h2>
<ul>
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/113_adds_user_avatar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddsUserAvatar < ActiveRecord::Migration
def up
say "Adds user avatar"
add_column :users, :resource_id, :integer
end

def down
say "Adds user avatar"
remove_column :users, :resource_id
end
end
55 changes: 35 additions & 20 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 112) do
ActiveRecord::Schema.define(:version => 113) do

create_table "articles_tags", :id => false, :force => true do |t|
t.integer "article_id"
Expand All @@ -25,14 +25,14 @@

create_table "categories", :force => true do |t|
t.string "name"
t.integer "position"
t.integer "position", :default => 0, :null => false
t.string "permalink"
t.text "keywords"
t.text "description"
t.integer "parent_id"
end

add_index "categories", ["permalink"], :name => "index_categories_on_permalink"
add_index "categories", ["permalink"], :name => "categories_permalink_index"

create_table "categorizations", :force => true do |t|
t.integer "article_id"
Expand Down Expand Up @@ -65,8 +65,8 @@
t.string "post_type", :default => "read"
end

add_index "contents", ["published"], :name => "index_contents_on_published"
add_index "contents", ["text_filter_id"], :name => "index_contents_on_text_filter_id"
add_index "contents", ["published"], :name => "contents_published_index"
add_index "contents", ["text_filter_id"], :name => "contents_text_filter_id_index"

create_table "feedback", :force => true do |t|
t.string "type"
Expand All @@ -92,22 +92,28 @@
t.string "user_agent"
end

add_index "feedback", ["article_id"], :name => "index_feedback_on_article_id"
add_index "feedback", ["text_filter_id"], :name => "index_feedback_on_text_filter_id"
add_index "feedback", ["article_id"], :name => "feedback_article_id_index"
add_index "feedback", ["text_filter_id"], :name => "feedback_text_filter_id_index"

create_table "link_articles_tags", :force => true do |t|
t.integer "article"
t.integer "tag"
t.integer "tag_count"
end

create_table "page_caches", :force => true do |t|
t.string "name"
t.string "name", :null => false
end

add_index "page_caches", ["name"], :name => "index_page_caches_on_name"
add_index "page_caches", ["name"], :name => "name"

create_table "pings", :force => true do |t|
t.integer "article_id"
t.string "url"
t.datetime "created_at"
end

add_index "pings", ["article_id"], :name => "index_pings_on_article_id"
add_index "pings", ["article_id"], :name => "article_id"

create_table "post_types", :force => true do |t|
t.string "name"
Expand Down Expand Up @@ -157,10 +163,11 @@
end

create_table "sidebars", :force => true do |t|
t.integer "active_position"
t.text "config"
t.integer "staged_position"
t.string "type"
t.integer "active_position"
t.text "config"
t.integer "staged_position"
t.datetime "updated_at"
t.string "type"
end

create_table "sitealizer", :force => true do |t|
Expand Down Expand Up @@ -188,6 +195,11 @@
t.text "params"
end

create_table "text_link_ads_rss", :force => true do |t|
t.string "html", :limit => 1024
t.integer "post_id"
end

create_table "triggers", :force => true do |t|
t.integer "pending_item_id"
t.string "pending_item_type"
Expand All @@ -196,20 +208,23 @@
end

create_table "users", :force => true do |t|
t.string "login"
t.string "password"
t.text "email"
t.text "name"
t.string "login", :limit => 80
t.string "password", :limit => 40
t.string "name", :limit => 80
t.string "email", :limit => 80
t.boolean "notify_via_email"
t.boolean "notify_on_new_articles"
t.boolean "notify_on_comments"
t.integer "profile_id"
t.string "remember_token"
t.datetime "remember_token_expires_at"
t.string "text_filter_id", :default => "1"
t.string "state", :default => "active"
t.string "text_filter_id", :default => "1"
t.string "state", :default => "active"
t.datetime "last_connection"
t.text "settings"
t.integer "resource_id"
end

add_index "users", ["login"], :name => "login", :unique => true

end
Loading

0 comments on commit 52c4b33

Please sign in to comment.