@song.font_size = Song.default_font_size
end
+ def show
+ @song = Song.find params[:id]
+ end
+
def create
- render :action => :new unless params[:song]
+ return render :action => :new unless params[:song]
@song = Song.new(params[:song])
unless @song.valid?
p @song.errors
redirect_to songs_path if @song.blank?
end
+ def update
+ @song = Song.active.find_by_id(params[:id])
+ @song.attributes = params[:song]
+ @song.save
+ return redirect_to :action => :edit
+ end
+
def list
@songs = Song.active.all
end
end
end.join " "
end
+
+ def format_words(song)
+ song.ruby.gsub("\n"){ "<br />" }.gsub("^( )+"){ " " * $1.length }
+ end
end
+# coding: utf-8
class Song < ActiveRecord::Base
include ::SelectableAttr::Base
+ before_save :make_words_for_search
selectable_attr :font_size do
- entry "0", :xsmall, 'XSmall'
- entry "1", :small, 'Small'
- entry "2", :middle, 'Middle'
- entry "3", :large, 'Large'
- entry "4", :xlarge, 'XLarge'
+ entry "0", :xsmall, "25pt"
+ entry "1", :small, "30pt"
+ entry "2", :middle, "34pt"
+ entry "3", :large, "39pt"
+ entry "4", :xlarge, "44pt"
end
+ RUBY_KANJI_HIRA = %r/([一-龠]+)\(([ぁ-ん]+)\)/
+ RUBY_ENGLISH_KANA = %r/([a-zA-Z]+)\(([ァ-ンー]+)\)/
+ RUBY_KANA_HIRA = %r/([ァ-ン]+)\(([ぁ-ん]+)\)/
+
CODES = %w(A Ab Bb C Cm D E Eb Em F F#m Fm G)
named_scope :active, :conditions => ["#{table_name}.deleted_at IS NULL"]
- validates_presence_of :titile, :words, :font_size
+ validates_presence_of :title, :words, :font_size
validates_numericality_of :font_size
def self.default_font_size
- self.font_size_id_by_key(:middle)
+ font_size_id_by_key(:middle)
+ end
+
+ def make_words_for_search
+ words_for_search = words + kana
+ end
+
+ def kana
+ strip_ruby_base(words)
+ end
+
+ def ruby
+ markup_html_ruby_tag(words)
+ end
+
+ # 慈(いつく)しみ
+ # => <ruby><rb>慈</rb><rp>(</rp><rt>いつく</rt><rp>)</rp></ruby>しみ
+ def markup_html_ruby_tag(str)
+ str.
+ to_s.
+ gsub(RUBY_KANJI_HIRA) { ruby_tag_template($1, $2) }.
+ gsub(RUBY_ENGLISH_KANA){ ruby_tag_template($1, $2) }.
+ gsub(RUBY_KANA_HIRA) { ruby_tag_template($1, $2) }
+ end
+
+ def ruby_tag_template(base, text)
+ "<ruby><rb>#{base}</rb><rp>(</rp><rt>#{text}</rt><rp>)</rp></ruby>"
+ end
+
+ def strip_ruby_base(str)
+ str.gsub(RUBY_KANJI_HIRA){ $2 }.gsub(RUBY_ENGLISH_KANA){ $2 }.gsub(RUBY_KANA_HIRA){ $2 }
end
end
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<%= stylesheet_link_tag "base" %>
+ <%= javascript_include_tag :defaults %>
<%= yield :head %>
</head>
<body>
</tr>
<tr>
<th>歌詞(WORDS)</th>
- <td><%= f.text_area :words %></td>
+ <td><%= f.text_area :words, :size => "40x8" %></td>
</tr>
<tr>
<th>出典(SOURCE)</th>
--- /dev/null
+<%= format_words(@song) %>
<% content_for :head do -%>
<title>Praise DATABASE</title>
- <%= javascript_include_tag "prototype" %>
+ <%= javascript_include_tag :defaults %>
<%= javascript_include_tag "roman" %>
<% end -%>
<h1>歌詞の編集 (edit this song)</h1>
-<% form_for @song, song_path(@song), :method => :put do |f| -%>
+<% form_for @song do |f| -%>
<%= render :partial => 'form', :locals => {:f => f} %>
- <%= f.text_area :kana, :style => "display:none" %>
<% end -%>
+<div id="song_kana" style="display:none;"><%=h @song.kana %></div>
-<% form_for @song, song_path, :method => :delete do |f| -%>
- <p>このデータを<%= f.submit "削除する" %></p>
-<% end -%>
-[<%= link_to "表示", song_path(@song) %>]
+<%= button_to "このデータを削除する", @song, :confirm => '本当に削除しますか?', :method => :delete %>
+[<%= link_to "表示", @song %>]
[<%= link_to "印刷用画面", print_song_path(@song) %>]
[<%= link_to "トップに戻る", song_path %>]
[<%= link_to_function "ローマ字", "toRoman()" %>]
[<%= link_to_function "リセット", "reset()" %>]
+
<hr />
+<%= format_words(@song) %>
--- /dev/null
+<%- content_for :head do -%>
+ <%= stylesheet_link_tag "show_song" %>
+ <%= javascript_include_tag "show_song" %>
+<script type="text/javascript">
+Event.observe(window, 'load', function() {
+ init(<%= @song.font_size_name %>);
+});
+</script>
+<%- end -%>
+<div id="fixed">
+ <pre id="smallWindow"><%= render :partial => 'fixed' %></pre>
+ <table id="panel">
+ <tr>
+ <th rowspan="2">文字の大きさの調整 </th>
+ <td>
+ <button onclick="javascript:increase();">▲</button>
+ </td>
+ </tr>
+ <td>
+ <button onclick="javascript:decrease();">▼</button>
+ </td>
+ </tr>
+ <tr>
+ <th colspan="2" id="b">
+ <a href="preview.php?id=<?= $id ?>" target="edit"> 編集画面を開く </a>
+ <!-- <button onclick="javascript:openEditWindow()">OPEN</button> -->
+ </th>
+ </tr>
+ </table>
+</div>
+<div id="scroll">
+ <pre id="song"><%= render :partial => 'scroll' %></pre>
+</div>
--- /dev/null
+var dragging = false;
+var top;
+var dragStartTop;
+var smallWindow;
+var song;
+
+function init(defaultFontSize) {
+ initDragging();
+ initSizeControll(defaultFontSize);
+}
+
+
+// º¸Ã¼¤Î½Ì¾®É½¼¨¤ò¥Þ¥¦¥¹¤Î¥É¥é¥Ã¥°¤Ç¥¹¥¯¥í¡¼¥ë
+
+function initDragging() {
+ var fixed = document.getElementById("fixed");
+ fixed.onmousedown = startMove;
+ fixed.onmousemove = processMove;
+ fixed.onmouseup = stopMove;
+ fixed.ondragstart = function () { return false; };
+ smallWindow = document.getElementById("smallWindow");
+}
+
+function startMove(event) {
+ if (!event) event = window.event;
+ dragStartLeft = event.clientX;
+ dragStartTop = event.clientY;
+ top = stripPx(smallWindow.style.top);
+ dragging = true;
+ return false;
+}
+
+function processMove(event) {
+ if (!event) event = window.event;
+ if (dragging) {
+ var pos = top + (event.clientY - dragStartTop);
+ if (pos < 10) smallWindow.style.top = pos + "px";
+ }
+ return false;
+}
+
+function stopMove(event) {
+ if (!event) event = window.event;
+ smallWindow.style.cursor = "";
+ dragging = false;
+}
+
+function stripPx(str) {
+ if (str == "") return 0;
+ return parseFloat(str.substring(0, str.length - 2));
+}
+
+
+// ²Î»ì¤Î¥Õ¥©¥ó¥È¥µ¥¤¥º¤òÄ´À᤹¤ë
+
+function initSizeControll(defaultFontSize) {
+ song = document.getElementById("song");
+ song.style.fontSize = defaultFontSize;
+}
+
+function increase() {
+ var size = parseInt(song.style.fontSize);
+ size += 2;
+ song.style.fontSize = size + "pt";
+}
+
+function decrease() {
+ var size = parseInt(song.style.fontSize);
+ size -= 2;
+ song.style.fontSize = size + "pt";
+}
+
+
+// ÊÔ½¸²èÌ̤òÊÌ¥¦¥£¥ó¥É¥¦¤Çɽ¼¨
+
+function openEditWindow() {
+ window.open(location.href.replace('view', 'preview'), '_edit');
+}
--- /dev/null
+#fixed {
+ position: fixed;
+ top: 0; left: 0;
+ padding: 10px;
+}
+#smallWindow {
+ position: relative;
+ top: 0; left: 0;
+ font-size: 9pt;
+ cursor: n-resize;
+}
+#panel {
+ position: absolute;
+ top: 50px; left: 300px;
+ font-size: 16pt;
+ width: 11em;
+ border: solid 1px;;
+}
+#b {
+ padding: 8px;
+ border-top: solid 1px;
+}
+#scroll {
+ float: right;
+ width: 950px;
+ padding: 50px 20px;
+ font-size: 20pt;
+ cursor: crosshair;
+}
+# coding: utf-8
require 'spec_helper'
describe Song do
before(:each) do
- @valid_attributes = {
- :code => "value for code",
- :title => "value for title",
- :words => "value for words",
- :words_for_search => "value for kana",
- :copyright => "value for cright",
- :font_size => 1,
- :deleted_at => Time.now
- }
+ @song = Song.new({
+ :words => "慈(いつく)しみ深(ふか)い 友(とも)なるイエスは"
+ })
end
- it "should create a new instance given valid attributes" do
- Song.create!(@valid_attributes)
- end
+ subject { @song }
+
+ its(:kana) {should == "いつくしみふかい ともなるイエスは"}
end