Skip to content

Commit

Permalink
User option for select with a display_names from user (Only user can
Browse files Browse the repository at this point in the history
know names that he can use)
  • Loading branch information
Yannick Francois committed Aug 12, 2013
1 parent b41d064 commit 3bb3516
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
2 changes: 0 additions & 2 deletions app/controllers/admin/profiles_controller.rb
@@ -1,6 +1,4 @@
class Admin::ProfilesController < Admin::BaseController
helper Admin::UsersHelper

def index
@user = current_user
@profiles = Profile.find(:all, :order => 'id')
Expand Down
14 changes: 0 additions & 14 deletions app/helpers/admin/users_helper.rb

This file was deleted.

14 changes: 11 additions & 3 deletions app/models/user.rb
Expand Up @@ -43,7 +43,7 @@ class User < ActiveRecord::Base
setting :twitter_oauth_token, :string, ''
setting :twitter_oauth_token_secret, :string, ''
setting :twitter_profile_image, :string, ''

# echo "publify" | sha1sum -
class_attribute :salt

Expand All @@ -58,6 +58,14 @@ def initialize(*args)
self.settings ||= {}
end

def first_and_last_name
return '' unless firstname.present? && lastname.present?
"#{firstname} #{lastname}"
end

def display_names
[:login, :nickname, :firstname, :lastname, :first_and_last_name].map{|f| send(f)}.delete_if{|e| e.empty?}
end

def self.authenticate(login, pass)
where("login = ? AND password = ? AND state = ?", login, password_hash(pass), 'active').first
Expand Down Expand Up @@ -181,7 +189,7 @@ def permalink
def admin?
profile.label == Profile::ADMIN
end

def update_twitter_profile_image(img)
return if self.twitter_profile_image == img
self.twitter_profile_image = img
Expand All @@ -200,7 +208,7 @@ def twitter_configured?
return false if self.twitter_oauth_token_secret.nil? or self.twitter_oauth_token_secret.empty?
true
end

protected

# Apply SHA1 encryption to the supplied password.
Expand Down
6 changes: 3 additions & 3 deletions app/views/admin/users/_form.html.erb
Expand Up @@ -70,7 +70,7 @@
<label class='control-label' for="user_login"><%= _("Display Name")%></label>
<div class='controls'>
<select name="user[name]">
<%= render_options_for_display_name %>
<%= options_for_select(@user.display_names, @user.name) %>
</select>
</div>
</div>
Expand Down Expand Up @@ -212,10 +212,10 @@
<div class='controls'>
<%= text_area('user', 'description', {:class => 'span5', :rows => 5}) %>
</div>
</div>
</div>
</fieldset>
<% end %>

<div class='form-actions'>
<%= cancel_or_save %>
</div>
</div>
38 changes: 38 additions & 0 deletions spec/models/user_spec.rb
Expand Up @@ -289,4 +289,42 @@ def set_password(newpass)
end
end

describe :first_and_last_name do
context "with first and last name" do
let(:user) { create(:user, firstname: 'Marlon', lastname: 'Brando') }
it { expect(user.first_and_last_name).to eq('Marlon Brando') }
end

context "with firstname without lastname" do
let(:user) { create(:user, firstname: 'Marlon', lastname: nil) }
it { expect(user.first_and_last_name).to eq('') }
end
end

describe :display_names do
context "with user without nickname, firstname, lastname" do
let(:user) { create(:user, nickname: nil, firstname: nil, lastname: nil) }
it { expect(user.display_names).to eq([user.login]) }
end

context "with user with nickname without firstname, lastname" do
let(:user) { create(:user, nickname: 'Bob', firstname: nil, lastname: nil) }
it { expect(user.display_names).to eq([user.login, user.nickname]) }
end

context "with user with firstname, without nickname, lastname" do
let(:user) { create(:user, nickname: nil, firstname: 'Robert', lastname: nil) }
it { expect(user.display_names).to eq([user.login, user.firstname]) }
end

context "with user with lastname, without nickname, firstname" do
let(:user) { create(:user, nickname: nil, firstname: nil, lastname: 'Redford') }
it { expect(user.display_names).to eq([user.login, user.lastname]) }
end

context "with user with firstname and lastname, witjout nickname" do
let(:user) { create(:user, nickname: nil, firstname: 'Robert', lastname: 'Redford') }
it { expect(user.display_names).to eq([user.login, user.firstname, user.lastname, "#{user.firstname} #{user.lastname}"]) }
end
end
end

0 comments on commit 3bb3516

Please sign in to comment.