Class Word
In: app/models/word.rb
Parent: ActiveRecord::Base

Methods

Included Modules

KotobaModel

Constants

REQUIRE_UNIQUE_SPELLING = false
MAX_NUMBER_OF_ATTEMPTS = 10
WORD_BASE_URL = 'http://kotoba.wardosworld.com/word_of_moment/'

Public Class methods

Select a word randomly of a specified language. If not specified then we default to Japanese.

We avoid words that have already been tweeted in the past 10 weeks.

[Source]

    # File app/models/word.rb, line 48
48:   def self.get_word_of_moment(language = Language.get_japanese)
49:     number_of_attempts = 0
50:     while (number_of_attempts < MAX_NUMBER_OF_ATTEMPTS)
51:       # rand() is mysql specific
52:       # if protege SQL try random()
53:       random_word = Word.find(:first, :conditions => ['language_id = ?', language.id], :order => 'rand()')
54: 
55:       tweets_newer_than = 10.weeks.ago
56:       source = TwitterTweet::SOURCE_MOMENT_OF
57:       recent_word_tweet = TwitterTweet.find(
58:         :first, 
59:         :conditions => ['word_id = ? and created_at >= ? and source = ?', random_word.id, tweets_newer_than, source], 
60:         :order => 'created_at desc')
61:         
62:       if random_word && (not recent_word_tweet)
63:         return random_word 
64:       else
65:         number_of_attempts += 1
66:       end
67:     end
68:     raise("Unable to randomly find a new word after [#{number_of_attempts}] tries")
69:   end

[Source]

    # File app/models/word.rb, line 71
71:   def self.get_word_of_moment_message
72:     begin
73:         random_word = get_word_of_moment
74:         word_of_day = random_word.word
75:         message     = "ことば「#{word_of_day}」:#{WORD_BASE_URL}#{random_word.id}"
76:         return [random_word, message]
77:     rescue
78:       raise("Unable to create a word of moment message due to: #{$!}")
79:     end
80:   end

[Source]

   # File app/models/word.rb, line 5
5:   def self.overview
6:     KotobaModel.overview(self)
7:   end

Public Instance methods

[Source]

     # File app/models/word.rb, line 99
 99:   def characters
100:     unique_characters = Hash.new
101:     record.spelling.each_char{ |character| unique_characters[character] = character }
102:     Character.find(:all, :conditions => ['character in ?'. unique_characters.keys])
103:   end

[Source]

    # File app/models/word.rb, line 82
82:   def sentences
83:     return [] if spelling.empty?
84:     Sentence.find(:all, :conditions => ['sentence like ? and language_id = ?',  "%#{spelling}%", language_id])
85:   end

[Source]

    # File app/models/word.rb, line 91
91:   def to_label
92:     "#{spelling}【#{pronunciation}】"
93:   end

[Source]

    # File app/models/word.rb, line 95
95:   def word
96:     "#{spelling}【#{pronunciation}】"    
97:   end

[Source]

    # File app/models/word.rb, line 87
87:   def word_references
88:     WordReference.find(:all, :conditions => ['word_id = ? or reference_word_id = ?', self.id, self.id ])
89:   end

Protected Instance methods

[Source]

     # File app/models/word.rb, line 107
107:   def authorized_for_create?
108:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_ADMIN)
109:   end

[Source]

     # File app/models/word.rb, line 115
115:   def authorized_for_destroy?
116:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_ADMIN)
117:   end

[Source]

     # File app/models/word.rb, line 119
119:   def authorized_for_read?
120:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_USER)
121:   end

[Source]

     # File app/models/word.rb, line 111
111:   def authorized_for_update?
112:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_USER)
113:   end

[Source]

     # File app/models/word.rb, line 127
127:   def pronunciation_authorized_for_read?
128:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_USER)
129:   end

[Source]

     # File app/models/word.rb, line 131
131:   def pronunciation_authorized_for_update?
132:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_ADMIN)
133:   end

[Source]

     # File app/models/word.rb, line 135
135:   def spelling_authorized_for_read?
136:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_USER)
137:   end

[Source]

     # File app/models/word.rb, line 139
139:   def spelling_authorized_for_update?
140:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_ADMIN)
141:   end

[Source]

     # File app/models/word.rb, line 123
123:   def vocabulary_lists_authorized?
124:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_USER)
125:   end

[Validate]