Class: School

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/school.rb

Constant Summary collapse

ZIPCODE_PATTERN =
/^[\d]{5}$/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.learner_form_search(zipcode, user_created_school = nil) ⇒ Object

handles zipcode searches from learner-side forms; will filter out schools that are not approved by the admin UNLESS that school is passed via the user_created_school parameter, which allows the unapproved school created by the current user to be returned as a part of the search query



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/school.rb', line 32

def self.learner_form_search(zipcode, user_created_school = nil)
  return [] unless zipcode && zipcode.match(ZIPCODE_PATTERN)

  schools = where('zipcode = ?', zipcode).where('approved = true').order('name asc').all
  
  # the school the user created should be included in the returned list only if these conditions are met
  if user_created_school && user_created_school.zipcode == zipcode && !user_created_school.approved
    schools << user_created_school
    schools.sort! { |a,b| a.name <=> b.name } # sort the user_created_school into the right location
  end

  schools
end

.render_form?Boolean

Class methods

Returns:

  • (Boolean)


24
25
26
# File 'app/models/school.rb', line 24

def self.render_form?
  count > 0 and Audience.count > 0
end

.search(name_or_zipcode) ⇒ Object



46
47
48
# File 'app/models/school.rb', line 46

def self.search(name_or_zipcode)
  name_or_zipcode ? where('name LIKE ? OR zipcode LIKE ?', "%#{name_or_zipcode}%", "%#{name_or_zipcode}%") : scoped
end

.without_school(school) ⇒ Object



50
51
52
# File 'app/models/school.rb', line 50

def self.without_school(school)
  where('schools.id != ?', school.id)
end

Instance Method Details

#migrate_users_to(school) ⇒ Object

moves all users associated with this school to school parameter, returns an array of all users migrated from this school to the school parameter

Raises:

  • (ArgumentException)


58
59
60
61
62
63
64
65
66
# File 'app/models/school.rb', line 58

def migrate_users_to(school)
  raise ArgumentException unless school

  profiles.map do |profile| 
    profile.school = school
    profile.save
    profile.user # returned through method call via an array (from map)
  end
end

#phoneObject



72
73
74
75
# File 'app/models/school.rb', line 72

def phone
  p = read_attribute :phone
  (p.nil? || p.size < 10) ? p : "(#{p[0,3]}) #{p[3,3]}-#{p[6,4]}#{p.size > 10 ? 'x' + p[10..-1]: ''}"
end

#phone=(val) ⇒ Object



68
69
70
# File 'app/models/school.rb', line 68

def phone=(val)
  write_attribute :phone, val.nil? ? '' : val.split(//).select{|char| char.match(/^\d$/) }.join.gsub(/^1/, '')
end

#to_client_model_jsonObject



77
78
79
# File 'app/models/school.rb', line 77

def to_client_model_json
  to_json :except => [:created_at, :updated_at]
end

#user_countObject



81
82
83
# File 'app/models/school.rb', line 81

def user_count
  profiles.count
end