Class CharacterRadical
In: app/models/character_radical.rb
Parent: ActiveRecord::Base

Methods

Public Class methods

Returns all the character radical combinations where a character matches all radicals

[Source]

    # File app/models/character_radical.rb, line 53
53:   def self.find_characters(radical_ids)
54:     if radical_ids.empty?
55:       return []
56:     else
57:       query = "SELECT\nid, character_id, radical_id, count(character_id) as character_frequency\nFROM\ncharacter_radicals\nWHERE\nradical_id in (\#{radical_ids.join(', ')})\nGROUP BY\ncharacter_id\nHAVING\ncharacter_frequency = \#{radical_ids.size}\n"
58:       return self.find_by_sql(query)
59:     end
60:   end

[Source]

    # File app/models/character_radical.rb, line 15
15:   def self.find_distinct
16:     time_since_last_update_in_seconds                      = Time.new - @@distinct_radicals_updated_at
17:     is_time_to_update = time_since_last_update_in_seconds >= @@distinct_radicals_update_every_seconds
18:     
19:     if @@distinct_radicals.empty? || is_time_to_update
20:       @@distinct_radicals = self.find(
21:         :all, 
22:         :select => "distinct(radical_id)").map{ |radical| radical.radical }.sort{ |a,b| a.stroke_count.to_i <=> b.stroke_count.to_i}
23:     end
24:     
25:     return @@distinct_radicals
26:   end

[Source]

    # File app/models/character_radical.rb, line 28
28:   def self.find_distinct_remaining_radical_ids(radical_ids)
29:     unique_radical_ids = Hash.new
30:     if radical_ids
31:       matching_character_radicals = self.find_characters(radical_ids.keys)
32:       unique_characters = matching_character_radicals.map{ |record| record.character_id }
33:       if not unique_characters.empty?
34:         query = "SELECT\ndistinct(radical_id) as radical_id\nFROM\ncharacter_radicals\nWHERE\ncharacter_id in (\#{unique_characters.join(', ')})\n"
35:         self.find_by_sql(query).each{ |record| 
36:           unique_radical_ids[record.radical_id.to_s] = true
37:         }    
38:       end
39:     end
40:     return unique_radical_ids
41:   end

Public Instance methods

[Source]

    # File app/models/character_radical.rb, line 11
11:   def to_label
12:     radical.character
13:   end

Protected Instance methods

[Source]

    # File app/models/character_radical.rb, line 76
76:   def authorized_for_create?
77:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_ADMIN)
78:   end

[Source]

    # File app/models/character_radical.rb, line 84
84:   def authorized_for_destroy?
85:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_ADMIN)
86:   end

[Source]

    # File app/models/character_radical.rb, line 88
88:   def authorized_for_read?
89:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_USER)
90:   end

[Source]

    # File app/models/character_radical.rb, line 80
80:   def authorized_for_update?
81:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_ADMIN)
82:   end

[Validate]