Skip to content

Commit

Permalink
cleanup models
Browse files Browse the repository at this point in the history
  • Loading branch information
semmons99 committed Mar 26, 2012
1 parent 2ed4db6 commit 2d03cca
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 36 deletions.
11 changes: 7 additions & 4 deletions app.rb
Expand Up @@ -52,7 +52,10 @@ def register_errors(messages)
end

if request.path_info =~ /^\/(preview|download|render)$/
@chordpro = ChordPro.new(params[:markup], params[:docname])
@doc = Doc.new(
markup: params[:markup],
name: params[:name]
)
end

flush_errors
Expand All @@ -79,17 +82,17 @@ def register_errors(messages)
end

post "/preview" do
@chordpro.render
@doc.render
end

post "/download" do
txt = ChordProTXT.new(@chordpro)
txt = ChordProTXT.new(@doc)

send_file(txt.path, filename: txt.name, type: "text/plain")
end

post "/render" do
pdf = ChordProPDF.new(@chordpro)
pdf = ChordProPDF.new(@doc)

send_file(pdf.path, filename: pdf.name, type: "application/pdf")
end
Expand Down
18 changes: 0 additions & 18 deletions models/chord_pro.rb

This file was deleted.

13 changes: 8 additions & 5 deletions models/chord_pro_file.rb
Expand Up @@ -12,9 +12,12 @@ def self.processor(&block)

attr_reader :name

def initialize(chordpro)
@name = "#{chordpro.name}#{extension}"
@file = generate(chordpro)
def initialize(doc)
name = doc.name
name = "chordpro" if name.nil? || name.strip == ""

@name = "#{name}#{extension}"
@file = generate(doc)
end

def path
Expand All @@ -31,9 +34,9 @@ def processor

private

def generate(chordpro)
def generate(doc)
tmp = Tempfile.new(name)
processor.call(chordpro, tmp) unless processor.nil?
processor.call(doc, tmp) unless processor.nil?
tmp
end
end
4 changes: 2 additions & 2 deletions models/chord_pro_pdf.rb
@@ -1,8 +1,8 @@
class ChordProPDF < ChordProFile
extension "pdf"

processor do |chordpro, pdf|
ps = ChordProPS.new(chordpro)
processor do |doc, pdf|
ps = ChordProPS.new(doc)
system("ps2pdf #{ps.path} #{pdf.path}")
end
end
4 changes: 2 additions & 2 deletions models/chord_pro_ps.rb
@@ -1,8 +1,8 @@
class ChordProPS < ChordProFile
extension "ps"

processor do |chordpro, ps|
txt = ChordProTXT.new(chordpro)
processor do |doc, ps|
txt = ChordProTXT.new(doc)
system("chordii -o #{ps.path} #{txt.path}")
end
end
4 changes: 2 additions & 2 deletions models/chord_pro_txt.rb
@@ -1,8 +1,8 @@
class ChordProTXT < ChordProFile
extension "txt"

processor do |chordpro, txt|
txt.write(chordpro.markup)
processor do |doc, txt|
txt.write(doc.markup)
txt.close
end
end
9 changes: 9 additions & 0 deletions models/doc.rb
@@ -1,6 +1,15 @@
require "rest_client"

class Doc < ActiveRecord::Base
belongs_to :user

validates_presence_of :user_id, :name
validates_uniqueness_of :name, scope: :user_id

def render
RestClient.post(
"http://tenbyten.com/cgi-bin/webchord.pl",
chordpro: markup
)
end
end
6 changes: 3 additions & 3 deletions views/edit.haml
Expand Up @@ -8,10 +8,10 @@
%form#new-form(action="" method="post")
%p
- if doc.name.blank?
%input#docname(name="docname" type="text" placeholder="ChordPro Name")
%input#name(name="name" type="text" placeholder="ChordPro Name")
- else
%input#docname(name="docname" type="text" value="#{doc.name}" disabled="true")
%input#docname(name="docname" type="hidden" value="#{doc.name}")
%input#name(name="name" type="text" value="#{doc.name}" disabled="true")
%input#name(name="name" type="hidden" value="#{doc.name}")
%a#rename-show(href="#") Rename
%a#rename-hide(href="#" style="display:none") Cancel
%p
Expand Down

0 comments on commit 2d03cca

Please sign in to comment.