Skip to content

Commit

Permalink
fix song and lyric import and creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kellen hart committed Oct 20, 2013
1 parent 1ba8341 commit c3d1143
Show file tree
Hide file tree
Showing 15 changed files with 490 additions and 484 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/models/song.js.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Pah.Song = DS.Model.extend
name: DS.attr()
lyrics: DS.attr()
chords: DS.attr()
date_written: DS.attr()
albums: DS.hasMany('album')
song_albums: DS.hasMany('songAlbum')
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/templates/lyrics_only.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{!-- {{#linkTo "song" this titleBinding=name}} --}}
<li>
<h2>{{name}}</h2>
<p>{{{formatted_lyrics}}}</p>
<p class="pre">{{{formatted_lyrics}}}</p>
</li>
{{!-- {{/linkTo}} --}}
{{/each}}
Expand Down
1 change: 0 additions & 1 deletion app/assets/javascripts/templates/song.hbs.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<span><h4>{{name}}</h4></span>
<span><%= audio_tag 'section_7.mp3', controls: true%></span>
<p>{{{formatted_lyrics}}}</p>
<p>{{chords}}</p>
<p>{{date_written}}</p>
</div>
</div>
4 changes: 4 additions & 0 deletions app/assets/stylesheets/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ ul {
list-style-type: none;
}

.pre {
white-space:pre; /* or pre-wrap if you want wrapping to still work. */
}

// foundation overrides
//...

Expand Down
2 changes: 1 addition & 1 deletion app/models/album.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class Album < ActiveRecord::Base
has_many :song_albums
has_many :songs, through: :song_albums

validates :date_recorded, :description, :title, presence: true
validates :date_recorded, :title, presence: true
end
4 changes: 2 additions & 2 deletions app/models/song.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Song < ActiveRecord::Base
attr_accessible :chords, :date_written, :lyrics, :name
attr_accessible :date_written, :lyrics, :name, :capo_instructions

has_many :song_albums
has_many :albums, through: :song_albums

validates :chords, :date_written, :lyrics, :name, presence: true
validates :lyrics, :name, presence: true
end
2 changes: 1 addition & 1 deletion app/models/song_album.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class SongAlbum < ActiveRecord::Base
attr_accessible :track_number, :album_id, :song_id
attr_accessible :track_number, :album_id, :song_id, :album, :song

belongs_to :album
belongs_to :song
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/song_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class SongSerializer < ActiveModel::Serializer
attributes :id, :name, :lyrics, :chords, :date_written
attributes :id, :name, :lyrics, :date_written

has_many :albums, key: :albums, embed: :ids
has_many :song_albums, key: :song_albums, embed: :ids
Expand Down
8 changes: 4 additions & 4 deletions db/migrate/20130830064515_create_songs.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class CreateSongs < ActiveRecord::Migration
def change
create_table :songs do |t|
t.string :name
t.text :lyrics
t.text :chords
t.datetime :date_written
t.string :name
t.text :lyrics
t.string :capo_instructions
t.datetime :date_written

t.timestamps
end
Expand Down
12 changes: 6 additions & 6 deletions db/migrate/20130908044718_create_albums.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class CreateAlbums < ActiveRecord::Migration
def change
create_table :albums do |t|
t.string :title
t.datetime :date_recorded
t.string :image_path
t.text :description
t.string :bandcamp_name
t.string :bandcamp_id
t.string :title
t.datetime :date_recorded
t.string :image_path
t.text :description
t.string :bandcamp_name
t.string :bandcamp_id

t.timestamps
end
Expand Down
6 changes: 3 additions & 3 deletions db/migrate/20130908045348_create_song_albums.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class CreateSongAlbums < ActiveRecord::Migration
def change
create_table :song_albums do |t|
t.references :album
t.references :song
t.integer :track_number
t.references :album
t.references :song
t.integer :track_number

t.timestamps
end
Expand Down
8 changes: 4 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
t.string "image_path"
t.text "description"
t.string "bandcamp_name"
t.integer "bandcamp_id"
t.string "bandcamp_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
Expand All @@ -38,10 +38,10 @@
create_table "songs", :force => true do |t|
t.string "name"
t.text "lyrics"
t.text "chords"
t.string "capo_instructions"
t.datetime "date_written"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end
34 changes: 4 additions & 30 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,23 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
require 'faker'
require 'seeds_helper'
require 'seeds_development_words'
# require 'seeds_production_data'
require 'seeds_production_data'
include SeedsHelper

ALBUM_BANDCAMP_NUMBERS = ['3132328764', '987155316']
ALBUM_BANDCAMP_NAMES = ["rainbow-gardens","sing-me-home"] #10 songs / 9 songs
ActiveRecord::Base.transaction do
songs = []
albums = []

14.times do
songs << Song.create!(
chords: "A B C\n1 2 3#{(80+rand(23)).chr}",
date_written: Time.now - rand(200).days,
lyrics: make_lyrics(verses: "~4"),
name: make_title
)
end

2.times do |n|
albums << Album.create!(
title: make_title,
description: Faker::Lorem.paragraph(5),
date_recorded: Time.now - rand(200).days,
title: ALBUM_NAMES[n],
date_recorded: ALBUM_DATES[n],
image_path: "album_#{n}",
bandcamp_name: ALBUM_BANDCAMP_NAMES[n],
bandcamp_id: ALBUM_BANDCAMP_NUMBERS[n]
)
end

# attach the songs to an album
song_sets = [(0..8), (9..13)]
2.times do |album_number|
track_counter_offset = song_sets[album_number].first - 1

song_sets[album_number].each do |song_number|
SongAlbum.create!(
album_id: albums[album_number].id,
song_id: songs[song_number].id,
track_number: song_number - track_counter_offset
)
end
end
parse_production_lyrics
end
82 changes: 57 additions & 25 deletions lib/seeds_helper.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,69 @@
module SeedsHelper
def make_lyrics(opts)
num_verses = opts[:verses] || 3 + rand(3)
lines_per_verse = opts[:lines_per_verse] || 3 + rand(5)
words_per_line = opts[:words_per_line] || 3 + rand(7)
lyrics = ""

if num_verses.is_a?(String) && num_verses[0] == "~"
num_verses = num_verses[1..-1].to_i
num_verses = num_verses/2 + rand(num_verses)
else
num_verses = num_verses.to_i
end
ALBUM_BANDCAMP_NUMBERS = ['987155316', '3132328764']
ALBUM_BANDCAMP_NAMES = ["sing-me-home", "rainbow-gardens"] #7 songs / 10 songs
ALBUM_NAMES = ["Sing Me Home", "Rainbow Gardens"]
ALBUM_DATES = [Date.new(2001, 8, 1), Date.new(2013, 8, 1)]

num_verses.times { lyrics << construct_a_verse(lines_per_verse, words_per_line) }
def parse_production_lyrics
lyrics_array = PRODUCTION_LYRICS.lines.to_a
song_count = 0

lyrics.strip
loop do
song_attrs = {}
song_count <= 9 ? album_index = 0 : album_index = 1

song_attrs[:name], song_attrs[:date_written], song_attrs[:capo_instructions] =
get_name_date_capo( lyrics_array.shift )

# move past empty lines
lyrics_array.shift until lyrics_array.first.strip != ""

# get lyrics
song_attrs[:lyrics] = get_lyrics(lyrics_array)
song = Song.create!(song_attrs)

song_set = SongAlbum.create!(
album: Album.find_by_bandcamp_name(ALBUM_BANDCAMP_NAMES[album_index]),
song: song,
track_number: song_count % 10 + 1
)

song_count += 1
break if lyrics_array.empty?
end
end

def construct_a_verse(lines_per_verse, words_per_line)
verse = ""
lines_per_verse.times do
words_per_line.times { verse << "#{WORDS_ARRAY.sample} " }
verse << "\n"
def get_name_date_capo(string)
metadata_array = string.strip.split(/\s{3,}/)

name = metadata_array.shift
begin
date_string = metadata_array.pop
date_written = Date::strptime(date_string,"%Y") unless metadata_array.empty?
rescue Error
puts "error on #{date_string}"
end
capo_instructions = metadata_array.pop unless metadata_array.empty?

verse << "\n"
[name, date_written, capo_instructions]
end

def make_title
num_words = 1 + rand(4)
title = ""
num_words.times { title << "#{WORDS_ARRAY.sample} "}
def get_lyrics(lyrics_array)
empty_lines_in_a_row = 0
next_line = nil
lyrics = ""
while empty_lines_in_a_row < 4
next_line = lyrics_array.shift
return lyrics if next_line == nil

title.strip
if next_line.strip == ""
empty_lines_in_a_row += 1
else
empty_lines_in_a_row = 0
end
lyrics += next_line
end

lyrics.strip
end
end
Loading

0 comments on commit c3d1143

Please sign in to comment.