From 028ef7843685e2b51b2502d2e248cf80b39a4b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Bucho=CC=88ster?= Date: Fri, 10 Jun 2011 02:16:30 +0200 Subject: [PATCH] added user controller and views, started to customize devise controllers --- .../devise_auth/registrations_controller.rb | 31 +++++++++++++++++++ .../devise_auth/sessions_controller.rb | 15 +++++++++ app/controllers/users_controller.rb | 8 +++++ app/helpers/users_helper.rb | 8 +++++ app/models/playlist.rb | 1 + app/models/user.rb | 7 +++-- .../{edit.html.erb => _edit.html.erb} | 0 .../{new.html.erb => _new.html.erb} | 0 .../sessions/{new.html.erb => _new.html.erb} | 0 app/views/devise/shared/_links.erb | 4 +-- app/views/layouts/application.html.haml | 4 +-- app/views/users/_edit.html.haml | 2 ++ app/views/users/_show.html.haml | 17 ++++++++++ 13 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 app/controllers/devise_auth/registrations_controller.rb create mode 100644 app/controllers/devise_auth/sessions_controller.rb create mode 100644 app/controllers/users_controller.rb create mode 100644 app/helpers/users_helper.rb rename app/views/devise/registrations/{edit.html.erb => _edit.html.erb} (100%) rename app/views/devise/registrations/{new.html.erb => _new.html.erb} (100%) rename app/views/devise/sessions/{new.html.erb => _new.html.erb} (100%) create mode 100644 app/views/users/_edit.html.haml create mode 100644 app/views/users/_show.html.haml diff --git a/app/controllers/devise_auth/registrations_controller.rb b/app/controllers/devise_auth/registrations_controller.rb new file mode 100644 index 0000000..42acf0d --- /dev/null +++ b/app/controllers/devise_auth/registrations_controller.rb @@ -0,0 +1,31 @@ +class DeviseAuth::RegistrationsController < Devise::RegistrationsController + + def new + resource = build_resource({}) + respond_with_navigational(resource) { render :partial => "devise/registrations/new" } + end + + def edit + render :partial => "devise/registrations/edit" + end + + def create + build_resource + + if resource.save + if resource.active_for_authentication? + set_flash_message :notice, :signed_up if is_navigational_format? + sign_in(resource_name, resource) + redirect_to user_path(resource) + else + set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format? + expire_session_data_after_sign_in! + redirect_to user_path(resource) + end + else + clean_up_passwords(resource) + respond_with_navigational(resource) { render :partial => "devise/registrations/new" } + end + end + +end \ No newline at end of file diff --git a/app/controllers/devise_auth/sessions_controller.rb b/app/controllers/devise_auth/sessions_controller.rb new file mode 100644 index 0000000..1019466 --- /dev/null +++ b/app/controllers/devise_auth/sessions_controller.rb @@ -0,0 +1,15 @@ +class DeviseAuth::SessionsController < Devise::SessionsController + + def new + resource = build_resource + clean_up_passwords(resource) + respond_with_navigational(resource, stub_options(resource)){ render :partial => "devise/sessions/new" } + end + + def create + resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new") + set_flash_message(:notice, :signed_in) if is_navigational_format? + sign_in(resource_name, resource) + redirect_to "/#!/home", :partial => "devise/registrations/new" + end +end \ No newline at end of file diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..49e280e --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,8 @@ +class UsersController < ApplicationController + def show + @user = User.find params[:id] + puts @user.inspect + render :partial => "show", :locals => { :user => @user } + end + +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..ae1b217 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,8 @@ +module UsersHelper + + def validate_avatar_url url + return url if url.present? and FileTest.exists? url + "examples/user-big.jpg" + end + +end diff --git a/app/models/playlist.rb b/app/models/playlist.rb index b1b32c9..2dcc59f 100644 --- a/app/models/playlist.rb +++ b/app/models/playlist.rb @@ -1,3 +1,4 @@ class Playlist < ActiveRecord::Base has_many :tracks, :through => :playlists_tracks + belongs_to :user end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 097a845..57c0b1b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,9 +1,10 @@ class User < ActiveRecord::Base has_and_belongs_to_many :artists + has_many :playlist - # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable + # :token_authenticatable, :encryptable, :confirmable, :lockable, :recoverable, :timeoutable devise :database_authenticatable, :registerable, :omniauthable, - :recoverable, :rememberable, :trackable, :validatable + :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :username, :email, :password, :password_confirmation, :remember_me @@ -35,4 +36,4 @@ class User < ActiveRecord::Base end end -end +end \ No newline at end of file diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/_edit.html.erb similarity index 100% rename from app/views/devise/registrations/edit.html.erb rename to app/views/devise/registrations/_edit.html.erb diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/_new.html.erb similarity index 100% rename from app/views/devise/registrations/new.html.erb rename to app/views/devise/registrations/_new.html.erb diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/_new.html.erb similarity index 100% rename from app/views/devise/sessions/new.html.erb rename to app/views/devise/sessions/_new.html.erb diff --git a/app/views/devise/shared/_links.erb b/app/views/devise/shared/_links.erb index 5c8263e..f1612bc 100644 --- a/app/views/devise/shared/_links.erb +++ b/app/views/devise/shared/_links.erb @@ -1,9 +1,9 @@ <%- if controller_name != 'sessions' %> - <%= link_to "Sign in", new_session_path(resource_name) %>
+ <%= link_to "Sign in", "/#!/login" %>
<% end -%> <%- if devise_mapping.registerable? && controller_name != 'registrations' %> - <%= link_to "Sign up", new_registration_path(resource_name) %>
+ <%= link_to "Sign up", "/#!/register" %>
<% end -%> <%- if devise_mapping.recoverable? && controller_name != 'passwords' %> diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 41421c8..ed23abc 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -27,7 +27,7 @@ %li.active= link_to "home", "/#!/home" %li= link_to "charts", "/#!/charts" %li= link_to "about", "#" - %li= link_to "register", "#" + %li= link_to "register", "/#!/register" %li.search = form_tag '/search', :method => 'get', :id => "search_form", :remote=>true do = text_field_tag 'q', params[:q], :class => "text", :placeholder => "Search here ..." @@ -74,4 +74,4 @@ %li %h3 %div= "Firework" - %span= "Katie Perry" \ No newline at end of file + %span= "Katie Perry" diff --git a/app/views/users/_edit.html.haml b/app/views/users/_edit.html.haml new file mode 100644 index 0000000..fd5a9eb --- /dev/null +++ b/app/views/users/_edit.html.haml @@ -0,0 +1,2 @@ +%h1 Users#edit +%p Find me in app/views/users/edit.html.haml \ No newline at end of file diff --git a/app/views/users/_show.html.haml b/app/views/users/_show.html.haml new file mode 100644 index 0000000..c245250 --- /dev/null +++ b/app/views/users/_show.html.haml @@ -0,0 +1,17 @@ +#user_left + =image_tag validate_avatar_url(user.avatar.url), :class => "picture" + .information + %h1= user.username + %ul.nav + %li=image_tag "icons/artist-info.png" + %li=image_tag "icons/artist-links.png" + %ul.con + %li="#{user.playcount} Plays" + %li="#{user.playlist.count} Playlists" +.tabs.tabs_js.w02 + %ul.nav + %li profile + %li playlist + .content + %div SONGS + %div ALBUMS \ No newline at end of file