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 end


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


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


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


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


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


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


send_file(pdf.path, filename: pdf.name, type: "application/pdf") send_file(pdf.path, filename: pdf.name, type: "application/pdf")
end 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 attr_reader :name


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

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


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


private private


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


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


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


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

class Doc < ActiveRecord::Base class Doc < ActiveRecord::Base
belongs_to :user belongs_to :user


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

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

0 comments on commit 2d03cca

Please sign in to comment.