Skip to content
Browse files

cleanup models

  • Loading branch information...
1 parent 2ed4db6 commit 2d03cca468333bbe25cae652077b0e4e168796e9 @semmons99 committed Mar 26, 2012
Showing with 33 additions and 36 deletions.
  1. +7 −4 app.rb
  2. +0 −18 models/chord_pro.rb
  3. +8 −5 models/chord_pro_file.rb
  4. +2 −2 models/chord_pro_pdf.rb
  5. +2 −2 models/chord_pro_ps.rb
  6. +2 −2 models/chord_pro_txt.rb
  7. +9 −0 models/doc.rb
  8. +3 −3 views/edit.haml
View
11 app.rb
@@ -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
@@ -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
View
18 models/chord_pro.rb
@@ -1,18 +0,0 @@
-require "rest_client"
-
-class ChordPro
- attr_reader :markup, :name
-
- def initialize(markup, name)
- @markup = markup
- @name = name
- @name = "chordpro" if name.nil? || name.strip == ""
- end
-
- def render
- RestClient.post(
- "http://tenbyten.com/cgi-bin/webchord.pl",
- chordpro: markup
- )
- end
-end
View
13 models/chord_pro_file.rb
@@ -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
@@ -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
View
4 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
View
4 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
View
4 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
View
9 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
View
6 views/edit.haml
@@ -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

0 comments on commit 2d03cca

Please sign in to comment.
Something went wrong with that request. Please try again.