Class: StudentInputFormControllerCreate

Inherits:
Object
  • Object
show all
Defined in:
lib/source/controllers/student_input_form/student_input_form_controller_create.rb

Overview

Контроллер для модального окна создания студента

Instance Method Summary collapse

Constructor Details

#initialize(parent_controller) ⇒ StudentInputFormControllerCreate

Returns a new instance of StudentInputFormControllerCreate.



10
11
12
13
# File 'lib/source/controllers/student_input_form/student_input_form_controller_create.rb', line 10

def initialize(parent_controller)
  @parent_controller = parent_controller
  LoggerHolder.instance.debug('StudentInputFormControllerCreate: initialized')
end

Instance Method Details

#on_view_createdObject

Вызывается из view после ее создания



23
24
25
26
27
28
29
# File 'lib/source/controllers/student_input_form/student_input_form_controller_create.rb', line 23

def on_view_created
  begin
    @student_rep = StudentRepository.new(DBSourceAdapter.new)
  rescue Mysql2::Error::ConnectionError => e
    on_db_conn_error(e)
  end
end

#process_fields(fields) ⇒ Object

Обработать данные из полей и добавить студента



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/source/controllers/student_input_form/student_input_form_controller_create.rb', line 34

def process_fields(fields)
  begin
    last_name = fields.delete(:last_name)
    first_name = fields.delete(:first_name)
    father_name = fields.delete(:father_name)

    return if last_name.nil? || first_name.nil? || father_name.nil?

    student = Student.new(last_name, first_name, father_name, **fields)

    LoggerHolder.instance.debug('StudentInputFormControllerCreate: adding student to DB')

    @student_rep.add_student(student)

    @view.close
  rescue ArgumentError => e
    LoggerHolder.instance.debug("StudentInputFormControllerCreate: wrong fields: #{e.message}")
    api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
    api.call(0, e.message, 'Error', 0)
  end
end

#set_view(view) ⇒ Object



15
16
17
18
# File 'lib/source/controllers/student_input_form/student_input_form_controller_create.rb', line 15

def set_view(view)
  @view = view
  LoggerHolder.instance.debug('StudentInputFormControllerCreate: view set')
end