From 09b44f0e6e2c48249518e536fdf2d9a9a260d476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Bucho=CC=88ster?= Date: Thu, 30 Jun 2011 06:38:27 +0200 Subject: [PATCH] finished user functionality, added favourite artists --- .gitignore | 7 +++- Gemfile.lock | 20 +++++----- app/controllers/application_controller.rb | 20 +++++++++- app/controllers/artists_controller.rb | 18 ++++++++- .../devise_auth/sessions_controller.rb | 9 ++++- app/controllers/home_controller.rb | 4 +- .../users/omniauth_callbacks_controller.rb | 6 ++- app/controllers/users_controller.rb | 10 ++++- app/helpers/application_helper.rb | 10 +++-- app/helpers/users_helper.rb | 6 +-- app/models/user.rb | 10 ++++- app/views/artists/_show.html.haml | 2 +- app/views/devise/shared/_links.erb | 8 ++-- app/views/home/_index.html.haml | 2 +- app/views/home/index.html.haml | 4 -- app/views/layouts/application.html.haml | 11 +++--- app/views/users/_edit.html.haml | 19 ++++----- app/views/users/_show.html.haml | 19 +++++---- config/routes.rb | 35 +++++++---------- config/sevendigital.yml | 39 ------------------- .../20110630033512_add_image_url_to_artist.rb | 9 +++++ db/schema.rb | 3 +- public/javascripts/ajax/favourites.js | 8 ++++ public/javascripts/ajax/requests.js | 16 ++++++++ public/javascripts/ajax/routes.js | 13 ++----- public/javascripts/config.js | 1 + 26 files changed, 175 insertions(+), 134 deletions(-) delete mode 100644 config/sevendigital.yml create mode 100644 db/migrate/20110630033512_add_image_url_to_artist.rb create mode 100644 public/javascripts/ajax/favourites.js diff --git a/.gitignore b/.gitignore index 1d0189a..3273b78 100644 --- a/.gitignore +++ b/.gitignore @@ -5,10 +5,13 @@ tmp/ Icon* .DS_Store +dump.rdb Thumbs.db /config/config.yml -/config/sevendigital.yml /public/images_old/ -/public/.sass-cache/ \ No newline at end of file +/public/.sass-cache/ +/public/media/ +/public/assets/ +/public/cache/ \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 45de663..1a2526f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -40,10 +40,11 @@ GEM bcrypt-ruby (2.1.4) bcrypt-ruby (2.1.4-x86-mingw32) builder (2.1.2) - closure (1.2.701) + closure (1.3.0) rack (>= 1.0.0) + cocaine (0.1.0) daemons (1.0.10) - devise (1.3.4) + devise (1.4.0) bcrypt-ruby (~> 2.1.2) orm_adapter (~> 0.0.3) warden (~> 1.0.3) @@ -64,10 +65,10 @@ GEM i18n (0.6.0) jammit (0.6.3) yui-compressor (>= 0.9.3) - jquery-rails (1.0.9) + jquery-rails (1.0.12) railties (~> 3.0) thor (~> 0.14) - json (1.5.1) + json (1.5.3) mail (2.2.19) activesupport (>= 2.3.6) i18n (>= 0.4.0) @@ -94,15 +95,16 @@ GEM oa-core (= 0.2.6) rack-openid (~> 1.3.1) ruby-openid-apps-discovery (~> 1.2.0) - oauth (0.4.4) + oauth (0.4.5) oauth2 (0.4.1) faraday (~> 0.6.1) multi_json (>= 0.0.5) - open4 (1.0.1) + open4 (1.1.0) orm_adapter (0.0.5) - paperclip (2.3.11) + paperclip (2.3.12) activerecord (>= 2.3.0) activesupport (>= 2.3.2) + cocaine (>= 0.0.2) peachy (0.4.1) polyglot (0.3.1) rack (1.2.3) @@ -135,7 +137,7 @@ GEM ruby-openid (>= 2.1.7) ruby_parser (2.0.6) sexp_processor (~> 3.0) - sass (3.1.2) + sass (3.1.3) sexp_processor (3.0.5) sqlite3 (1.3.3) sqlite3 (1.3.3-x86-mingw32) @@ -147,7 +149,7 @@ GEM typhoeus (0.2.4) mime-types mime-types - tzinfo (0.3.27) + tzinfo (0.3.29) warden (1.0.4) rack (>= 1.0) will_paginate (2.3.15) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7e51c88..a08a26f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,22 @@ protect_from_forgery before_filter { |c| @youtube_client = YouTubeIt::Client.new(:dev_key=>"AI39si600H1fRQ6HW1pHG89e-23Uxf44YHu5TsCJMhNf5hFaYWSSSyyrOIEcOX451sFeh_OZdbbA48_g6OInfTv042-Id163tg") } -end + + def async_redirect_to path + redirect_to "/#!" + path + end + + def validate_img_url image, type, size + @attr = (type=="gallery") ? "name" : "size" + image.each do |img| + if img[@attr] == size.to_s + if img["#text"].blank? + return "/images/placeholder/#{type.to_s}.png" + end + return img["#text"] + end + end + + end + +end \ No newline at end of file diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 732be83..751f6c9 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -5,7 +5,7 @@ class ArtistsController < ApplicationController @@limit = { tracks: 10, albums: 4, similar: 6 } def show - artist = CGI.unescape params[:artist] + artist = CGI.unescape params[:id] @artist = LastFM::Artist.getInfo artist @tracks = LastFM::Artist.getTopTracks artist, @@limit[:tracks] @@ -44,12 +44,26 @@ class ArtistsController < ApplicationController end def album_info - @album = LastFM::Album.getInfo CGI.unescape(params[:album]), CGI.unescape(params[:artist]) + @album = LastFM::Album.getInfo CGI.unescape(params[:album]), CGI.unescape(params[:id]) LastFM::Request.run_queue! render :partial=>"album_info", :locals=>{:album=>@album} end + def favourize + puts params.inspect + artist = CGI.unescape params[:id] + @artist = LastFM::Artist.getInfo artist + LastFM::Request.run_queue! + @artist = Artist.new(:name => @artist.name, :img_url => validate_img_url(@artist.image, "artist", :extralarge)) + unless current_user.artists.include? @artist + current_user.artists << @artist if @artist.save + end + render :nothing => true + end + + private + def prepare params params[:q] = CGI.unescape params[:q] params[:size] = params[:size].to_i diff --git a/app/controllers/devise_auth/sessions_controller.rb b/app/controllers/devise_auth/sessions_controller.rb index 1019466..34d1e4c 100644 --- a/app/controllers/devise_auth/sessions_controller.rb +++ b/app/controllers/devise_auth/sessions_controller.rb @@ -10,6 +10,13 @@ class DeviseAuth::SessionsController < Devise::SessionsController 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" + aysnc_redirect_to new_user_registration_path + end + + def destroy + signed_in = signed_in?(resource_name) + Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name) + + async_redirect_to charts_path end end \ No newline at end of file diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 4d67b7b..e1beab2 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,8 +1,8 @@ class HomeController < ApplicationController - def show + def show @files = Dir.glob("public/images/startsite/*.jpg") - render :partial=>"index" + render :partial=>"index", :locals => {:files => @files} end end diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index c26a7a9..1dc7ffa 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -6,7 +6,8 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController if @user.persisted? flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook" - sign_in_and_redirect @user, :event => :authentication + sign_in @user + async_redirect_to user_path(@user) else session["devise.facebook_data"] = env["omniauth.auth"] redirect_to new_user_registration_url @@ -18,7 +19,8 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController @user = User.find_for_open_id(env["omniauth.auth"], current_user) if @user.persisted? flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google" - sign_in_and_redirect @user, :event => :authentication + sign_in @user + async_redirect_to user_path(@user) else session["devise.open:id_data"] = env["openid.ext1"] redirect_to new_user_registration_url diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 84c2bc1..39624ba 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,12 +1,18 @@ class UsersController < ApplicationController def show - @user = User.find params[:user] + @user = User.find params[:id] render :partial => "show", :locals => { :user => @user } end def edit - @user = User.find params[:user] + @user = User.find params[:id] render :partial => "edit", :locals => { :user => @user } end + + def update + current_user.update_attribute(:avatar, params[:user][:avatar]) if params[:user] and params[:user][:avatar] + current_user.update_attribute(:username, params[:user][:username]) if params[:user] and params[:user][:username] + async_redirect_to user_path(current_user) + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e590817..1cd8f6a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -24,8 +24,12 @@ module ApplicationHelper end def camelCaseString phrase - phrase.gsub!(/^[a-z]|\s+[a-z]/) { |a| a.upcase } - return phrase -end + phrase.gsub!(/^[a-z]|\s+[a-z]/) { |a| a.upcase } + return phrase + end + + def async_link_to body, path, html_options = {} + link_to body, "/#!" + path, html_options + end end \ No newline at end of file diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index ae1b217..9d0b87a 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -1,7 +1,7 @@ module UsersHelper - - def validate_avatar_url url - return url if url.present? and FileTest.exists? url + + def validate_avatar user, size = :original + return user.avatar.url(size) if user.avatar? "examples/user-big.jpg" end diff --git a/app/models/user.rb b/app/models/user.rb index 57c0b1b..82644bb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,8 +8,14 @@ class User < ActiveRecord::Base # Setup accessible (or protected) attributes for your model attr_accessible :username, :email, :password, :password_confirmation, :remember_me - has_attached_file :avatar, :styles => { :original => "200x200>", :thumb => "60x60>" } - + has_attached_file :avatar, + :styles => { + :original => "200x200#", + :thumb => "60x60#" + }, + :url => "/media/avatars/:style/:id_:filename", + :path => ":rails_root/public/media/avatars/:style/:id_:filename" + def self.new_with_session(params, session) super.tap do |user| if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["user_hash"] diff --git a/app/views/artists/_show.html.haml b/app/views/artists/_show.html.haml index 8e54022..c01f2c1 100644 --- a/app/views/artists/_show.html.haml +++ b/app/views/artists/_show.html.haml @@ -4,7 +4,7 @@ .tabs_js.information %h1 = artist.name - = link_to "", "#", :class=>"heart" + %span.heart %ul.nav %li= image_tag "icons/artist-info.png" %li= image_tag "icons/artist-links.png" diff --git a/app/views/devise/shared/_links.erb b/app/views/devise/shared/_links.erb index f1612bc..6f5efbe 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", "/#!/login" %>
+ <%= async_link_to "Login", new_user_session_path %>
<% end -%> <%- if devise_mapping.registerable? && controller_name != 'registrations' %> - <%= link_to "Sign up", "/#!/register" %>
+ <%= async_link_to "Register", new_user_registration_path %>
<% end -%> <%- if devise_mapping.recoverable? && controller_name != 'passwords' %> @@ -19,6 +19,6 @@ <% end -%> <%- if devise_mapping.omniauthable? %> - <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
- <%= link_to "Sign in with Google", user_omniauth_authorize_path(:open_id, :openid_url => 'https://www.google.com/accounts/o8/id') %>
+ <%= link_to "Login with Facebook", user_omniauth_authorize_path(:facebook) %>
+ <%= link_to "Login with Google", user_omniauth_authorize_path(:open_id, :openid_url => 'https://www.google.com/accounts/o8/id') %>
<% end -%> \ No newline at end of file diff --git a/app/views/home/_index.html.haml b/app/views/home/_index.html.haml index 986b0fe..268f4e4 100644 --- a/app/views/home/_index.html.haml +++ b/app/views/home/_index.html.haml @@ -1,5 +1,5 @@ %ul#top_artist - - for file in @files + - for file in files %li= image_tag "startsite/"+File.basename(file) %ul#top_artist_nav %li= link_to image_tag("startsite/small/beatles.jpg"),"", :class=>"ll dir" diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index b3d9f11..e69de29 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -1,4 +0,0 @@ -#white - %br - %br - %br \ No newline at end of file diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index f5239d9..61d6b4b 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -16,18 +16,17 @@ #site #uuser{:style=>"display:block; color:#ddd;"} - if user_signed_in? - =link_to "Profile", "/#!/users/#{current_user.id}" - =link_to "Logout", "/#!/logout" - - else - =link_to "Login", "/#!/login" - =link_to "Register", "/#!/register" + =async_link_to "Logout", destroy_user_session_path #head %h1= link_to image_tag("nine-minutes.png"), "/#!/home" = render :partial=>"layouts/player" #navigation %li.active= link_to "home", "/#!/home" %li= link_to "charts", "/#!/charts" - %li= link_to "register", "/#!/register" + - if signed_in? + %li= async_link_to "profile", user_path(current_user) + - else + %li= async_link_to "register", new_user_registration_path %li.search = form_tag '/search', :method => 'get', :id => "search_form", :remote=>true do = text_field_tag 'q', params[:q], :class => "text", :placeholder => "Search here ..." diff --git a/app/views/users/_edit.html.haml b/app/views/users/_edit.html.haml index 1d23c8a..e601a79 100644 --- a/app/views/users/_edit.html.haml +++ b/app/views/users/_edit.html.haml @@ -1,12 +1,7 @@ -#user_left - =image_tag validate_avatar_url(user.avatar.url), :class => "picture" - .information - %p{:style=>"display:block; color:#ddd;"} - %h1{:style=>"color:#ddd;"}= current_user.username || current_user.email - %br - -.tabs.tabs_js.w02 - %ul.nav - %li profile settings - .content - %div EDIT - bla bla bla \ No newline at end of file +EDIT - Settings += form_for user, :url => {:action=>"update"} do |f| + =f.text_field :username + =f.submit "Save" += form_for user, :url => {:action=>"update"}, :html => {:multipart=>true} do |f| + =f.file_field :avatar + =f.submit "Save" \ No newline at end of file diff --git a/app/views/users/_show.html.haml b/app/views/users/_show.html.haml index 83ad174..c665a4c 100644 --- a/app/views/users/_show.html.haml +++ b/app/views/users/_show.html.haml @@ -1,10 +1,7 @@ #user_left - =image_tag validate_avatar_url(user.avatar.url), :class => "picture" + =image_tag validate_avatar(user), :class => "picture" .information - %h1= user.username - %p{:style=>"display:block; color:#ddd;"} - =link_to "Profil editieren", "/#!/users/#{current_user.id}/edit" - %br + %h1= user.username || current_user.email %ul.nav %li=image_tag "icons/artist-info.png" %li=image_tag "icons/artist-links.png" @@ -14,7 +11,13 @@ .tabs.tabs_js.w02 %ul.nav %li profile - %li playlist + - if user == current_user + %li settings .content - %div SONGS - %div ALBUMS \ No newline at end of file + %div + - user.artists.each do |artist| + %p + =artist.name + =image_tag artist.img_url + - if user == current_user + %div=render :partial => "edit", :locals => { :user => current_user } \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ee1018e..f1e0374 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,5 @@ Nineminutes::Application.routes.draw do - get "charts/index" - devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", :registrations => "registrations", :sessions => "sessions" } devise_scope :user do @@ -14,27 +12,24 @@ Nineminutes::Application.routes.draw do end get "tracks/index" - match "/artists/:artist", :to => "artists#show" + match "/search_video", :to => "tracks#search_video" + + resources :artists, :only => [ :show ], :constraints => { :id => /.*/ } match "/more_tracks", :to => "artists#more_tracks" match "/more_albums", :to => "artists#more_albums" - match "/more_artists", :to => "search#more_artists" - match "/more_charts", :to => "charts#more" - - #get "artists/", :to => "artists#index" - match "/users/:user", :to => "users#show" - match "/users/:user/edit", :to => "users#edit" - - - match "/autocomplete", :to => "search#autocomplete" match "/album_info", :to => "artists#album_info" - match "/search_video", :to => "tracks#search_video" - - #resources :artists, :constraints => { :id => /.*/ } - resources :search, :constraints => { :id => /.*/ } - #resources :users, :only => [ :show ] - - get "home/", :to => "home#show" - get "charts/", :to => "charts#index" + match "/favourize", :to => "artists#favourize" + + resources :search, :only => [ :show ], :constraints => { :id => /.*/ } + match "/more_artists", :to => "search#more_artists" + match "/autocomplete", :to => "search#autocomplete" + + match "/more_charts", :to => "charts#more" + match "/charts", :to => "charts#index" + + resources :users, :only => [ :show, :update, :edit, :create ] + + get "/home", :to => "home#show" root :to => "home#index" end diff --git a/config/sevendigital.yml b/config/sevendigital.yml deleted file mode 100644 index ff07c17..0000000 --- a/config/sevendigital.yml +++ /dev/null @@ -1,39 +0,0 @@ -#Your API access key and secret -oauth_consumer_key: "7d7gfjmr2r" -oauth_consumer_secret: "qhb5jq2pwycc7yhz" - -#Your website/application name & version -#this information will be appended to user-agent HTTP headers for all API requests -app_name: 9minutes -app_version: 0.0.1 - -#automatically populate properties -lazy_load: true - -#raise SevendigitalError if lazy loading a property fails? -#true - the error is ignored and the property remains unpopulated -#false - the original error is raised -ignorant_lazy_load: false - -#what information to log -#false or nil - (default) don't log anything -#true or verbose - log basic info -#very_verbose - include full request & api responses in the log -#verbose: true - -## default settings for common API parameters ## - -#if any of the bellow parameters is specified here it will be appended to all API requests -#they can always be overridden for each individual API query -#e.g.: -# @client = Sevendigital::Client.new(:country => 'GB', :page_size => 20) -# @client.release.search("radiohead", :country => 'ES', :page_size => 5) -> will return 5 results from spanish catalogue - -#end user's location -#country: GB - -##size of images returned by API responses (for valid size please see the 7digital API documentation) -#image_size: 50 - -#number of items returned in API responses -#page_size: 20 \ No newline at end of file diff --git a/db/migrate/20110630033512_add_image_url_to_artist.rb b/db/migrate/20110630033512_add_image_url_to_artist.rb new file mode 100644 index 0000000..e86fc0c --- /dev/null +++ b/db/migrate/20110630033512_add_image_url_to_artist.rb @@ -0,0 +1,9 @@ +class AddImageUrlToArtist < ActiveRecord::Migration + def self.up + add_column :artists, :img_url, :string + end + + def self.down + remove_column :artists, :img_url + end +end diff --git a/db/schema.rb b/db/schema.rb index 418df72..033efac 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,12 +10,13 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110606000240) do +ActiveRecord::Schema.define(:version => 20110630033512) do create_table "artists", :force => true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" + t.string "img_url" end create_table "artists_users", :id => false, :force => true do |t| diff --git a/public/javascripts/ajax/favourites.js b/public/javascripts/ajax/favourites.js new file mode 100644 index 0000000..b10737c --- /dev/null +++ b/public/javascripts/ajax/favourites.js @@ -0,0 +1,8 @@ +$(document).ready(function(){ + $('.heart').live("click", function(){ + var params = "id="+$('#content .tabs .content').attr('q'); + alert(params); + favourite_request(favourite_path, params); + }); + +}); \ No newline at end of file diff --git a/public/javascripts/ajax/requests.js b/public/javascripts/ajax/requests.js index 1517943..98f48d9 100644 --- a/public/javascripts/ajax/requests.js +++ b/public/javascripts/ajax/requests.js @@ -26,6 +26,22 @@ } } +function favourite_request(link, params){ + $.ajax({ + type: "GET", + dataType: "html", + url: link, + data: params, + error: function(){ + hide_flash(false); + show_flash(true); + }, + success: function(data){ + $('#content .heart').addClass('loved'); + } + }); +} + function load_site_request(link, params){ cacheResponse = cacheRequest(link+params); hide_flash(true); diff --git a/public/javascripts/ajax/routes.js b/public/javascripts/ajax/routes.js index 879c82d..8a343f9 100644 --- a/public/javascripts/ajax/routes.js +++ b/public/javascripts/ajax/routes.js @@ -1,9 +1,9 @@ $(document).ready(function(){ // Artist Show Page - Path.map(hashbang+artist_path+":artist").to(function(){ + Path.map(hashbang+artist_path+":id").to(function(){ show_flash(false); - load_site_request(artist_path, this.params['artist']); + load_site_request(artist_path, this.params['id']); }); // Home Page @@ -43,14 +43,9 @@ $(document).ready(function(){ }); // User Show Page - Path.map(hashbang+user_path+":user").to(function(){ + Path.map(hashbang+user_path+":id").to(function(){ show_flash(false); - load_site_request(user_path, this.params['user']); + load_site_request(user_path, this.params['id']); }); - // User Edit Page - Path.map(hashbang+user_path+":user/edit").to(function(){ - show_flash(false); - load_site_request(user_path, this.params['user'] + "/edit"); - }); }); \ No newline at end of file diff --git a/public/javascripts/config.js b/public/javascripts/config.js index 6e1e8c5..de6b5eb 100644 --- a/public/javascripts/config.js +++ b/public/javascripts/config.js @@ -11,6 +11,7 @@ var user_path = "/users/"; var register_path = "/register"; var login_path = "/login"; var logout_path = "/logout"; +var favourite_path = "/favourize"; $(document).ready(function(){ // home path