• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Révision1278c5a99a049447f2decef15ea0e8431512ff0c (tree)
l'heure2013-03-27 02:02:43
AuteurSHIMADA Keiki <shimada.cake@gmai...>
CommiterSHIMADA Keiki

Message de Log

ginmaku-2 songレコードの基本的なCLUDを実装する

歌詞が更新すると検索用カラムが更新される

Change Summary

Modification

--- a/Gemfile
+++ b/Gemfile
@@ -1,5 +1,4 @@
11 source 'https://rubygems.org'
2-source 'http://gems.github.com'
32
43
54 gem 'rails', '3.2.12'
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,6 +1,5 @@
11 GEM
22 remote: https://rubygems.org/
3- remote: http://gems.github.com/
43 specs:
54 actionmailer (3.2.12)
65 actionpack (= 3.2.12)
--- a/app/controllers/songs_controller.rb
+++ b/app/controllers/songs_controller.rb
@@ -77,9 +77,15 @@ class SongsController < ApplicationController
7777 # PUT /songs/1.json
7878 def update
7979 @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
8086
8187 respond_to do |format|
82- if @song.update_attributes(params[:song])
88+ if is_saved
8389 format.html { redirect_to @song, notice: 'Song was successfully updated.' }
8490 format.json { head :no_content }
8591 else
@@ -93,8 +99,7 @@ class SongsController < ApplicationController
9399 # DELETE /songs/1.json
94100 def destroy
95101 @song = Song.find(params[:id])
96- @song.deleted = true
97- @song.save
102+ @song.destroy
98103
99104 respond_to do |format|
100105 format.html { redirect_to songs_url }
--- a/app/helpers/songs_helper.rb
+++ b/app/helpers/songs_helper.rb
@@ -1,23 +1,20 @@
11 # coding: utf-8
22 module SongsHelper
3- # ふりがな処理のための正規表現
4- RUBY_REG1 = /([一-龠々]+)\(([ぁ-ん]+)\)/ # 漢字 ⇒ ひらがな
5- RUBY_REG2 = /([a-zA-Z']+)\(([ァ-ヶー]+)\)/ # 英語 ⇒ カタカナ
6- RUBY_REG3 = /([ァ-ヴー]+)\(([ぁ-んー]+)\)/ # カタカナ ⇒ ひらがな
73
84 def rubify(str)
95 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>"}.
139 html_safe
1410 end
1511
12+ # TODO Song#kanji で実装する
1613 def suppress_ruby(str)
1714 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}.
2118 html_safe
2219 end
2320
--- a/app/models/song.rb
+++ b/app/models/song.rb
@@ -1,6 +1,13 @@
1+# coding: utf-8
12 class Song < ActiveRecord::Base
23 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一-龠々ぁ-んァ-ヶ々ー]/ # 英字、漢字、ひらがな、カタカナ 以外の文字
411
512 def self.code_options
613 %w(A Ab Bb C Cm D E Eb Em F F#m Fm G)
@@ -19,4 +26,26 @@ class Song < ActiveRecord::Base
1926 def outline
2027 phrases.map{ |string| string.gsub(/\n.*/, '') }
2128 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
2251 end
\ No newline at end of file
--- a/app/models/song_search_form.rb
+++ b/app/models/song_search_form.rb
@@ -15,7 +15,7 @@ class SongSearchForm
1515
1616 def search
1717 songs = Song.arel_table
18- result = Song.where(songs[:kana].matches("%#{q}%"))
18+ result = Song.where(songs[:words_for_search].matches("%#{q}%"))
1919 if code.present?
2020 result = result.where(songs[:code].eq(code))
2121 end
@@ -23,5 +23,5 @@ class SongSearchForm
2323 end
2424
2525 private
26- def persisted?; false; end
26+ def persisted?; false end
2727 end
\ No newline at end of file
--- a/app/views/songs/search.html.erb
+++ b/app/views/songs/search.html.erb
@@ -7,3 +7,5 @@
77 </p>
88 <%- end -%>
99 <%= render :partial => 'list' %>
10+
11+<%= link_to 'Back', songs_path %>