diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 0000000..a2e98ad --- /dev/null +++ b/.rvmrc @@ -0,0 +1 @@ +rvm use --create 2.0.0-p0@capstone-deadsets \ No newline at end of file diff --git a/Gemfile b/Gemfile index b968d71..43532bc 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,8 @@ gem 'rails', '3.2.13' gem 'pg' gem 'devise' gem 'nokogiri' +gem "decent_exposure", "~> 2.2.0" +gem 'simple_form' # Gems used only for assets and not required # in production environments by default. @@ -25,6 +27,7 @@ group :test do gem 'cucumber-rails' gem 'database_cleaner' gem 'rspec-rails' + gem 'pry' end gem 'jquery-rails' diff --git a/Gemfile.lock b/Gemfile.lock index e6dd03b..2851926 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,7 @@ GEM nokogiri (>= 1.5.0) rails (~> 3.0) database_cleaner (1.0.1) + decent_exposure (2.2.0) devise (2.2.4) bcrypt-ruby (~> 3.0) orm_adapter (~> 0.1) @@ -133,6 +134,9 @@ GEM railties (~> 3.2.0) sass (>= 3.1.10) tilt (~> 1.3) + simple_form (2.1.0) + actionpack (~> 3.0) + activemodel (~> 3.0) slop (3.4.5) sprockets (2.2.2) hike (~> 1.2) @@ -160,12 +164,15 @@ DEPENDENCIES coffee-rails (~> 3.2.1) cucumber-rails database_cleaner + decent_exposure (~> 2.2.0) devise jquery-rails nokogiri pg + pry pry-rails rails (= 3.2.13) rspec-rails sass-rails (~> 3.2.3) + simple_form uglifier (>= 1.0.3) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index f94f80c..f1ce9a3 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -57,9 +57,26 @@ p { color: white; } +ul { + list-style-type: none; +} + +li { + font-family: 'RobotoThin', sans-serif; + font-size: 14px; + color: white; +} + +.info_part { + font-size: 10px; + color: #FFEAB5; + padding-left: 10px; + +} + a { font-family: 'RobotoThin', sans-serif; - font-size: 12px; + font-size: 14px; line-height: 20px; color: #b0b0af; text-decoration: none; diff --git a/app/controllers/concerts_controller.rb b/app/controllers/concerts_controller.rb index 6d65219..4bfc192 100644 --- a/app/controllers/concerts_controller.rb +++ b/app/controllers/concerts_controller.rb @@ -1,16 +1,34 @@ class ConcertsController < ApplicationController + expose(:concerts) do + if params[:search_by_date] + Concert.search_by_date(params[:search_by_date]) + elsif params[:search_by_venue] + Concert.search_by_venue(params[:search_by_venue]) + elsif params[:search_by_tour] + Concert.search_by_tour(params[:search_by_tour]) + else + concerts = todays_concerts + end + end + + expose(:concerts_count) { concerts.count } def index - @concerts = Concert.search(params[:search]) - if params[:search] + if concerts.blank? + flash[:notice] = "Please try again" + end + end + - if @concerts.blank? - flash[:notice] = "Sorry, there's no concert for this date" + def todays_concerts + concert_collection = Concert.all + concert_collection.collect! do |concert| + if concert.date.strftime('%m/%d') == Time.now.strftime('%m/%d') + concert end end - + concert_collection.reject! { |c| c.nil? } end - end diff --git a/app/controllers/songs_controller.rb b/app/controllers/songs_controller.rb new file mode 100644 index 0000000..d8a95fb --- /dev/null +++ b/app/controllers/songs_controller.rb @@ -0,0 +1,13 @@ +class SongsController < ApplicationController + expose(:song) { Song.find(params[:id]) } + + def new + @song = Song.new + end + + def create + @song.save! + end + +end + diff --git a/app/helpers/concerts_helper.rb b/app/helpers/concerts_helper.rb index 516b508..c967564 100644 --- a/app/helpers/concerts_helper.rb +++ b/app/helpers/concerts_helper.rb @@ -1,2 +1,34 @@ module ConcertsHelper -end + + def venue_options + concerts = Concert.all.map { |c| c } + concerts.uniq! { |c| c.venue } + concerts.sort_by { |c| c.venue } + end + + def venues_count + venue_options.count + end + + def songs_count + Song.all.count + end + + def tour_options + concerts = Concert.all.map { |c| c } + concerts.uniq! { |c| c.tour } + end + + def search_criteria + if params[:search_by_date] + params[:search_by_date] + elsif params[:search_by_venue] + params[:search_by_venue] + elsif params[:search_by_tour] + params[:search_by_tour] + else + "Today in Grateful Dead history" + end + end + +end \ No newline at end of file diff --git a/app/models/concert.rb b/app/models/concert.rb index a5bd025..ea284d5 100644 --- a/app/models/concert.rb +++ b/app/models/concert.rb @@ -1,9 +1,11 @@ class Concert < ActiveRecord::Base - attr_accessible :date, :details + attr_accessible :date, :venue, :tour + has_many :setlists + has_many :songs, through: :setlists has_many :users, through: :favorites has_many :favorites - def self.search(search) + def self.search_by_date(search) if search search_split = search.split("-") search_split.each do |search| @@ -12,10 +14,58 @@ def self.search(search) end end search = search_split.join("-").to_s - where(:date => search).all + where(date: search).all else [] + flash[:notice] = "Sorry there is no match for your search criteria" end end + def self.search_by_venue(search) + if search + search_split = search.split("-") + search_split.each do |search| + if search.starts_with? "0" + search.slice!(0) + end + end + search = search_split.join("-").to_s + where(venue: search).all + else + [] + flash[:notice] = "Sorry there is no match for your search criteria" + end + end + + def self.search_by_tour(search) + if search + search_split = search.split("-") + search_split.each do |search| + if search.starts_with? "0" + search.slice!(0) + end + end + search = search_split.join("-").to_s + where(tour: search).all + else + [] + flash[:notice] = "Sorry there is no match for your search criteria" + end + end + + def first_set + self.setlists.where(group: "Set 1") + end + + def second_set + self.setlists.where(group: "Set 2") + end + + def third_set + self.setlists.where(group: "Set 3") + end + + def encore + self.setlists.where(group: "Encore:") + end end \ No newline at end of file diff --git a/app/models/setlist.rb b/app/models/setlist.rb index a21db01..90adc54 100644 --- a/app/models/setlist.rb +++ b/app/models/setlist.rb @@ -1,3 +1,8 @@ class Setlist < ActiveRecord::Base - attr_accessible :showdate, :deadset + attr_accessible :concert_id, :song_id, :order, :group + belongs_to :concert + belongs_to :song + + delegate :group, to: :song, prefix: true + end diff --git a/app/models/song.rb b/app/models/song.rb new file mode 100644 index 0000000..ab8a607 --- /dev/null +++ b/app/models/song.rb @@ -0,0 +1,25 @@ +class Song < ActiveRecord::Base + attr_accessible :title, :media_link + has_many :setlists + has_many :concerts, through: :setlists + + + def times_played + self.concerts.count + end + + def first_time_played + "#{self.concerts.last.date.strftime('%m/%d/%Y')} at #{self.concerts.last.venue}" + end + + def last_time_played + "#{self.concerts.first.date.strftime('%m/%d/%Y')} at #{self.concerts.first.venue}" + end + + def avg_times_played + concerts = Concert.where("date >= ?", self.concerts.last.date) + total = ((times_played.to_f/concerts.count.to_f)*100).round(2) + "#{total}% of concerts since it was introduced in #{self.concerts.last.date.strftime('%Y')}" + end + +end \ No newline at end of file diff --git a/app/views/concerts/index.html.erb b/app/views/concerts/index.html.erb index 0329dc2..285cdc3 100644 --- a/app/views/concerts/index.html.erb +++ b/app/views/concerts/index.html.erb @@ -4,14 +4,54 @@ <% flash.discard(:notice) %> <% end %> - -
u - <% if (@concerts) %> - <% @concerts.each do |concert| %> +
+ <%- if concerts.present? %> +

<%= search_criteria %> - <%= concerts_count %>

+ <% concerts.each do |concert| %> + <%- if !concert.nil? %>

- <%= simple_format(concert.details) %> + <%= link_to "#{concert.date.strftime('%m/%d/%Y')} - #{concert.venue}", "#", :class=>"setlist_button" %>

- +
+ <% if concert.first_set.present? %> +
    +
  • Set 1
  • + <% concert.first_set.each do |set| %> +
  • <%= link_to set.song.title, "songs/#{set.song.id}" %><%= set.song.info %>
  • + <% end %> +
+
    +
  • Set 2
  • + <% concert.second_set.each do |set| %> +
  • <%= link_to set.song.title, "songs/#{set.song.id}" %><%= set.song.info %>
  • + <% end %> +
+ <% if concert.third_set.present? %> +
    +
  • Set 3
  • + <% concert.third_set.each do |set| %> +
  • <%= link_to set.song.title, "songs/#{set.song.id}" %><%= set.song.info %>
  • + <% end %> +
+ <% end %> +
    +
  • Encore
  • + <% concert.encore.each do |set| %> +
  • <%= link_to set.song.title, "songs/#{set.song.id}" %><%= set.song.info %>
  • + <% end %> +
+ <% else %> + <% if concert.setlists.empty? %> +

No songs have been recorded for this concert

+ <% else %> +
    + <% concert.setlists.each do |set| %> +
  • <%= link_to set.song.title, "songs/#{set.song.id}" %><%= set.song.info %>
  • + <% end %> +
+ <% end %> + <% end %> +
<% if user_signed_in? %> <%= form_for concert.favorites.new do |f| %> <%= f.label :favorite, :class=>"hidden_label" %> @@ -21,7 +61,6 @@ <% end %> <% end %> <% end %> - - + <% end %>
diff --git a/app/views/concerts/songs/show.html.erb b/app/views/concerts/songs/show.html.erb new file mode 100644 index 0000000..252f018 --- /dev/null +++ b/app/views/concerts/songs/show.html.erb @@ -0,0 +1 @@ +<%= song.title %> \ No newline at end of file diff --git a/app/views/favorites/index.html.erb b/app/views/favorites/index.html.erb index afedeb8..a600d6b 100644 --- a/app/views/favorites/index.html.erb +++ b/app/views/favorites/index.html.erb @@ -12,12 +12,49 @@ <% if @favorites.any? %>

Favorite Concerts

<% @favorites.each do |favorite| %> -

- <%= link_to favorite.concert.details[/.*/].chomp, "#", :class=>"setlist_button" %> -

-
- <%= simple_format favorite.setlist %> -
+

+ <%= link_to "#{favorite.concert.date} - #{favorite.concert.venue} - #{favorite.concert.tour}", "#", :class=>"setlist_button" %> +

+
+ <% if favorite.concert.first_set.present? %> + + + <% if favorite.concert.third_set.present? %> + + <% end %> + + <% else %> + <% if favorite.concert.setlists.empty? %> +

No songs have been recorded for this concert

+ <% else %> + + <% end %> + <% end %> +
<%= button_to "Delete", favorite, :method=>:delete, :class=>"deletebutton" %> <% end %> <% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c743347..7048438 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -33,12 +33,27 @@ <% unless a_devise_page? %>
-

Search for a Grateful Dead
concert date in mm-dd-yy format

+

Search by Date

<%= form_tag concerts_path, :method => 'get' do %> - <%= text_field_tag :search, params[:search] %> + <%= text_field_tag :search_by_date, params[:search_by_date] %> <%= submit_tag "Search", :class => "searchbutton", :name => nil %> <% end %>
+
+

Search by Venue (<%= venues_count %>)

+ <%= form_tag concerts_path, :method => 'get' do %> + <%= select_tag :search_by_venue, options_from_collection_for_select(venue_options, "venue", "venue"), prompt: "Select something" %> + <%= submit_tag "Search", :class => "searchbutton", :name => nil %> + <% end %> +
+
+

Search by Tour

+ <%= form_tag concerts_path, :method => 'get' do %> + <%= select_tag :search_by_tour, options_from_collection_for_select(tour_options, "tour", "tour"), prompt: "Select something" %> + <%= submit_tag "Search", :class => "searchbutton", :name => nil %> + <% end %> +
+
<% end %> diff --git a/app/views/songs/show.html.erb b/app/views/songs/show.html.erb new file mode 100644 index 0000000..5fd9f3f --- /dev/null +++ b/app/views/songs/show.html.erb @@ -0,0 +1,7 @@ +
+

<%= song.title %>

+

Was played at <%= song.times_played %> concerts

+

Introduced live on <%= song.first_time_played %>

+

Last played live on <%= song.last_time_played %>

+

Played on average <%= song.avg_times_played %>

+
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 807cf1e..1aaa621 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,9 @@ devise_for :users resources :concerts + resources :concert_sets + resources :setlists + resources :songs, only: [:create, :edit, :show, :index, :destroy] resources :favorites root :to => "concerts#index" diff --git a/db/migrate/20130615194944_create_setlists.rb b/db/migrate/20130615194944_create_setlists.rb index 0a1f270..364d19b 100644 --- a/db/migrate/20130615194944_create_setlists.rb +++ b/db/migrate/20130615194944_create_setlists.rb @@ -1,8 +1,10 @@ class CreateSetlists < ActiveRecord::Migration def change create_table :setlists do |t| - t.text :showdate - t.text :deadset + t.integer :concert_id + t.integer :song_id + t.integer :order + t.string :group t.timestamps end diff --git a/db/migrate/20130617184735_create_concerts.rb b/db/migrate/20130617184735_create_concerts.rb index ef38c00..7bee2bd 100644 --- a/db/migrate/20130617184735_create_concerts.rb +++ b/db/migrate/20130617184735_create_concerts.rb @@ -1,8 +1,9 @@ class CreateConcerts < ActiveRecord::Migration def change create_table :concerts do |t| - t.string :date - t.text :details + t.date :date + t.text :venue + t.text :tour t.timestamps end diff --git a/db/migrate/20130630155649_create_songs.rb b/db/migrate/20130630155649_create_songs.rb new file mode 100644 index 0000000..e8c38ac --- /dev/null +++ b/db/migrate/20130630155649_create_songs.rb @@ -0,0 +1,11 @@ +class CreateSongs < ActiveRecord::Migration + def change + create_table :songs do |t| + t.string :title + t.string :media_link + t.string :info + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 8407f06..f3fee87 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,11 +11,12 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130623202033) do +ActiveRecord::Schema.define(:version => 20130630155649) do create_table "concerts", :force => true do |t| - t.string "date" - t.text "details" + t.date "date" + t.text "venue" + t.text "tour" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end @@ -28,8 +29,18 @@ end create_table "setlists", :force => true do |t| - t.text "showdate" - t.text "deadset" + t.integer "concert_id" + t.integer "song_id" + t.integer "order" + t.string "group" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + create_table "songs", :force => true do |t| + t.string "title" + t.string "media_link" + t.string "info" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end @@ -47,7 +58,6 @@ t.string "last_sign_in_ip" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false - t.string "username" end add_index "users", ["email"], :name => "index_users_on_email", :unique => true diff --git a/lib/tasks/scrape_setlists.rake b/lib/tasks/scrape_setlists.rake new file mode 100644 index 0000000..c9e2feb --- /dev/null +++ b/lib/tasks/scrape_setlists.rake @@ -0,0 +1,56 @@ +desc 'Scrape grateful dead setlists from setlist.fm' +task :gd => :environment do + require 'nokogiri' + require 'open-uri' + # This range can be set up to 196. It must be set at 196 to retrieve all 1955 concerts. + range = (1..196).to_a + range.each do |i| + page = Nokogiri::HTML(open("http://www.setlist.fm/setlists/grateful-dead-bd6ad4a.html?page=#{i}")) + # Nokogiri supports CSS-style selectors + @links = [] + page.search('#id1d h2 a').each { |a| @links << a['href'] } + @links.each do |url| + url = url[1..-1] + url = url[1..-1] + concert_page = Nokogiri::HTML(open("http://www.setlist.fm#{url}")) + @date = concert_page.css(".dateBlock").text.delete("\n") + header = [] + concert_page.css(".eventBlock .value").each do |e| + header << e.text + end + @concert = Concert.create(date: @date, venue: header[1], tour: header[2]) + i = 1 + concert_page.css("ol li").each do |l| + if l.text.delete("\n").match("Set") || l.text.delete("\n").match("Encore:") + @group = l.text.delete("\n") + else + if l.children.children.children[0].present? + title = l.children.children.children[0].text + if l.children.children.children[2].present? + info = l.children.children.children[2].text + l.children.children.children[3].text + l.children.children.children[4].text + end + else + title = nil + end + song = Song.where(title: title, info: info).first_or_create + Setlist.create(song_id: song.id, order: i, concert_id: @concert.id, group: @group) + i += 1 + end + end + end + end + Setlist.where(group: "Set One").each do |set| + set.group = "Set 1" + set.save + end + Setlist.where(group: "Set Two").each do |set| + set.group = "Set 2" + set.save + end + song = Song.where(title: nil).first.destroy + Setlist.where(song_id: song.id).each do |set| + set.destroy + end + puts "Concerts: #{Concert.all.count}" + puts "Songs: #{Song.all.count}" +end \ No newline at end of file