2 class Song < ActiveRecord::Base
3 include ::SelectableAttr::Base
4 before_save :make_words_for_search
6 selectable_attr :font_size do
7 entry 0, :xsmall, "XSmall", :point => "25pt"
8 entry 1, :small, "Small", :point => "30pt"
9 entry 2, :middle, "Middle", :point => "34pt"
10 entry 3, :large, "Large", :point => "39pt"
11 entry 4, :xlarge, "XLarge", :point => "44pt"
14 RUBY_KANJI_HIRA = %r/([一-龠]+)\(([ぁ-ん]+)\)/
15 RUBY_ENGLISH_KANA = %r/([a-zA-Z]+)\(([ァ-ンー]+)\)/
16 RUBY_KANA_HIRA = %r/([ァ-ン]+)\(([ぁ-ん]+)\)/
18 CODES = %w(A Ab Bb C Cm D E Eb Em F F#m Fm G)
20 named_scope :active, :conditions => ["#{table_name}.deleted_at IS NULL"]
22 validates_presence_of :title, :words, :font_size
23 validates_numericality_of :font_size
25 def self.default_font_size
26 font_size_id_by_key(:middle)
29 def self.begin_with(letter)
30 self.find(:all, :conditions => ["words_for_search LIKE ?", letter + '%'], :order => "id")
33 def make_words_for_search
34 words_for_search = kana + words
38 strip_ruby_base(words)
42 strip_ruby_text(words)
46 markup_html_ruby_tag(ERB::Util.h words)
50 # => <ruby><rb>慈</rb><rp>(</rp><rt>いつく</rt><rp>)</rp></ruby>しみ
51 def markup_html_ruby_tag(str)
54 gsub(RUBY_KANJI_HIRA) { ruby_tag_template($1, $2) }.
55 gsub(RUBY_ENGLISH_KANA){ ruby_tag_template($1, $2) }.
56 gsub(RUBY_KANA_HIRA) { ruby_tag_template($1, $2) }.
60 def ruby_tag_template(base, text)
61 "<ruby><rb>#{base}</rb><rp>(</rp><rt>#{text}</rt><rp>)</rp></ruby>"
64 def strip_ruby_base(str)
65 str.gsub(RUBY_KANJI_HIRA){ $2 }.
66 gsub(RUBY_ENGLISH_KANA){ $2 }.
67 gsub(RUBY_KANA_HIRA){ $2 }.
71 def strip_ruby_text(str)
72 str.gsub(RUBY_KANJI_HIRA){ $1 }.
73 gsub(RUBY_ENGLISH_KANA){ $1 }.
74 gsub(RUBY_KANA_HIRA){ $1 }.