Révision | 1278c5a99a049447f2decef15ea0e8431512ff0c (tree) |
---|---|
l'heure | 2013-03-27 02:02:43 |
Auteur | SHIMADA Keiki <shimada.cake@gmai...> |
Commiter | SHIMADA Keiki |
ginmaku-2 songレコードの基本的なCLUDを実装する
歌詞が更新すると検索用カラムが更新される
@@ -1,5 +1,4 @@ | ||
1 | 1 | source 'https://rubygems.org' |
2 | -source 'http://gems.github.com' | |
3 | 2 | |
4 | 3 | |
5 | 4 | gem 'rails', '3.2.12' |
@@ -1,6 +1,5 @@ | ||
1 | 1 | GEM |
2 | 2 | remote: https://rubygems.org/ |
3 | - remote: http://gems.github.com/ | |
4 | 3 | specs: |
5 | 4 | actionmailer (3.2.12) |
6 | 5 | actionpack (= 3.2.12) |
@@ -77,9 +77,15 @@ class SongsController < ApplicationController | ||
77 | 77 | # PUT /songs/1.json |
78 | 78 | def update |
79 | 79 | @song = Song.find(params[:id]) |
80 | + is_saved = nil | |
81 | + Song.transaction do | |
82 | + @song.attributes = params[:song] | |
83 | + @song.update_words_for_search | |
84 | + is_saved = @song.save | |
85 | + end | |
80 | 86 | |
81 | 87 | respond_to do |format| |
82 | - if @song.update_attributes(params[:song]) | |
88 | + if is_saved | |
83 | 89 | format.html { redirect_to @song, notice: 'Song was successfully updated.' } |
84 | 90 | format.json { head :no_content } |
85 | 91 | else |
@@ -93,8 +99,7 @@ class SongsController < ApplicationController | ||
93 | 99 | # DELETE /songs/1.json |
94 | 100 | def destroy |
95 | 101 | @song = Song.find(params[:id]) |
96 | - @song.deleted = true | |
97 | - @song.save | |
102 | + @song.destroy | |
98 | 103 | |
99 | 104 | respond_to do |format| |
100 | 105 | format.html { redirect_to songs_url } |
@@ -1,23 +1,20 @@ | ||
1 | 1 | # coding: utf-8 |
2 | 2 | module SongsHelper |
3 | - # ふりがな処理のための正規表現 | |
4 | - RUBY_REG1 = /([一-龠々]+)\(([ぁ-ん]+)\)/ # 漢字 ⇒ ひらがな | |
5 | - RUBY_REG2 = /([a-zA-Z']+)\(([ァ-ヶー]+)\)/ # 英語 ⇒ カタカナ | |
6 | - RUBY_REG3 = /([ァ-ヴー]+)\(([ぁ-んー]+)\)/ # カタカナ ⇒ ひらがな | |
7 | 3 | |
8 | 4 | def rubify(str) |
9 | 5 | str. |
10 | - gsub(RUBY_REG1){"<ruby>#{$1}<rt>#{$2}</rt></ruby>"}. | |
11 | - gsub(RUBY_REG2){"<ruby>#{$1}<rt>#{$2}</rt></ruby>"}. | |
12 | - gsub(RUBY_REG3){"<ruby>#{$1}<rt>#{$2}</rt></ruby>"}. | |
6 | + gsub(Song::RUBY_REG1){"<ruby>#{$1}<rt>#{$2}</rt></ruby>"}. | |
7 | + gsub(Song::RUBY_REG2){"<ruby>#{$1}<rt>#{$2}</rt></ruby>"}. | |
8 | + gsub(Song::RUBY_REG3){"<ruby>#{$1}<rt>#{$2}</rt></ruby>"}. | |
13 | 9 | html_safe |
14 | 10 | end |
15 | 11 | |
12 | + # TODO Song#kanji で実装する | |
16 | 13 | def suppress_ruby(str) |
17 | 14 | str. |
18 | - gsub(RUBY_REG1){$1}. | |
19 | - gsub(RUBY_REG2){$1}. | |
20 | - gsub(RUBY_REG3){$1}. | |
15 | + gsub(Song::RUBY_REG1){$1}. | |
16 | + gsub(Song::RUBY_REG2){$1}. | |
17 | + gsub(Song::RUBY_REG3){$1}. | |
21 | 18 | html_safe |
22 | 19 | end |
23 | 20 |
@@ -1,6 +1,13 @@ | ||
1 | +# coding: utf-8 | |
1 | 2 | class Song < ActiveRecord::Base |
2 | 3 | acts_as_paranoid |
3 | - attr_accessible :code, :cright, :kana, :title, :words, :deleted_at, :updated_at | |
4 | + attr_accessible :code, :cright, :title, :words, :words_for_search, :deleted_at, :updated_at | |
5 | + | |
6 | + # ふりがな処理のための正規表現 | |
7 | + RUBY_REG1 = /([一-龠々]+)\(([ぁ-ん]+)\)/ # 漢字 ⇒ ひらがな | |
8 | + RUBY_REG2 = /([a-zA-Z']+)\(([ァ-ヶー]+)\)/ # 英語 ⇒ カタカナ | |
9 | + RUBY_REG3 = /([ァ-ヴー]+)\(([ぁ-んー]+)\)/ # カタカナ ⇒ ひらがな | |
10 | + RUBY_REG4 = /[^a-zA-Z一-龠々ぁ-んァ-ヶ々ー]/ # 英字、漢字、ひらがな、カタカナ 以外の文字 | |
4 | 11 | |
5 | 12 | def self.code_options |
6 | 13 | %w(A Ab Bb C Cm D E Eb Em F F#m Fm G) |
@@ -19,4 +26,26 @@ class Song < ActiveRecord::Base | ||
19 | 26 | def outline |
20 | 27 | phrases.map{ |string| string.gsub(/\n.*/, '') } |
21 | 28 | end |
29 | + | |
30 | + def kanji | |
31 | + words. | |
32 | + gsub(RUBY_REG1){$1}. | |
33 | + gsub(RUBY_REG2){$2}. | |
34 | + gsub(RUBY_REG2){$3} | |
35 | + end | |
36 | + | |
37 | + def kana | |
38 | + words. | |
39 | + gsub(RUBY_REG1){$2}. | |
40 | + gsub(RUBY_REG2){$2}. | |
41 | + gsub(RUBY_REG3){$3} | |
42 | + end | |
43 | + | |
44 | + def update_words_for_search | |
45 | + self[:words_for_search] = ruby_trim(kanji + kana) | |
46 | + end | |
47 | + | |
48 | + def ruby_trim(str) | |
49 | + str.gsub(RUBY_REG4, '') | |
50 | + end | |
22 | 51 | end |
\ No newline at end of file |
@@ -15,7 +15,7 @@ class SongSearchForm | ||
15 | 15 | |
16 | 16 | def search |
17 | 17 | songs = Song.arel_table |
18 | - result = Song.where(songs[:kana].matches("%#{q}%")) | |
18 | + result = Song.where(songs[:words_for_search].matches("%#{q}%")) | |
19 | 19 | if code.present? |
20 | 20 | result = result.where(songs[:code].eq(code)) |
21 | 21 | end |
@@ -23,5 +23,5 @@ class SongSearchForm | ||
23 | 23 | end |
24 | 24 | |
25 | 25 | private |
26 | - def persisted?; false; end | |
26 | + def persisted?; false end | |
27 | 27 | end |
\ No newline at end of file |
@@ -7,3 +7,5 @@ | ||
7 | 7 | </p> |
8 | 8 | <%- end -%> |
9 | 9 | <%= render :partial => 'list' %> |
10 | + | |
11 | +<%= link_to 'Back', songs_path %> |