Class: AuthorListController

Inherits:
Object
  • Object
show all
Defined in:
lib/author/controllers/author_list_controller.rb

Overview

Класс AuthorListController отвечает за управление списком авторов, используя различные методы для обновления данных и взаимодействия с пользовательским интерфейсом.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ AuthorListController

Returns a new instance of AuthorListController.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/author/controllers/author_list_controller.rb', line 17

def initialize(view)
  LoggerHolder.instance.debug('AuthorListController: initialize')
  @view = view
  @state_notifier = ListStateNotifier.new
  @state_notifier.add_listener(@view)
  @author_rep = AuthorDBDataSource.new

  @sort_columns = %w[AuthorID FirstName LastName FatherName]
  @sort_by = @sort_columns.first

  @father_name_filter_columns = [nil, true, false]
  @father_name_filter = @father_name_filter_columns.first
end

Instance Attribute Details

#state_notifierObject (readonly)

Returns the value of attribute state_notifier.



15
16
17
# File 'lib/author/controllers/author_list_controller.rb', line 15

def state_notifier
  @state_notifier
end

Instance Method Details

#delete_selected(current_page, per_page, selected_row) ⇒ Object

метод, который получает выбранный элемент из state_notifier, удаляет его из базы данных и из state_notifier



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/author/controllers/author_list_controller.rb', line 65

def delete_selected(current_page, per_page, selected_row)
  LoggerHolder.instance.debug('AuthorListController: delete_selected')
  begin
    item = @state_notifier.get(selected_row)
    @author_rep.delete(item.author_id)
    @state_notifier.delete(item)
  rescue
    api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
    api.call(0, "You cannot delete the author because he is associated with some book", "Error", 0)
  end
end

#filter_father_name(page, per_page, filter_index) ⇒ Object



95
96
97
98
# File 'lib/author/controllers/author_list_controller.rb', line 95

def filter_father_name(page, per_page, filter_index)
  @father_name_filter = @father_name_filter_columns[filter_index]
  refresh_data(page, per_page)
end

#on_view_createdObject



31
32
33
34
35
36
37
# File 'lib/author/controllers/author_list_controller.rb', line 31

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

#refresh_data(page, per_page) ⇒ Object

етод, который получает список авторов из базы данных, устанавливает их в state_notifier и обновляет пользовательский интерфейс



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/author/controllers/author_list_controller.rb', line 78

def refresh_data(page, per_page)
  # begin
  #   @data_list = @student_rep.paginated_short_students(page, per_page, @data_list)
  #   @view.update_student_count(@student_rep.student_count)
  # rescue
  #   on_db_conn_error
  # end
  items = @author_rep.get_list(per_page, page, @sort_by, 'ASC', @father_name_filter)
  @state_notifier.set_all(items)
  @view.update_student_count(@author_rep.count)
end

#show_modal_addObject



43
44
45
46
47
48
49
# File 'lib/author/controllers/author_list_controller.rb', line 43

def show_modal_add
  LoggerHolder.instance.debug('AuthorListController: show_modal_add')
  controller = AuthorInputFormControllerCreate.new(self)
  view = AuthorInputForm.new(controller)
  controller.set_view(view)
  view.create.show
end

#show_modal_edit(current_page, per_page, selected_row) ⇒ Object

метод, который создает контроллер AuthorInputFormControllerEdit, представление AuthorInputForm, устанавливает связи между ними и показывает модальное окно.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/author/controllers/author_list_controller.rb', line 52

def show_modal_edit(current_page, per_page, selected_row)
    LoggerHolder.instance.debug('AuthorListController: show_modal_edit')
  # item_num = (current_page - 1) * per_page + selected_row

  item = @state_notifier.get(selected_row)

  controller = AuthorInputFormControllerEdit.new(self, item)
  view = AuthorInputForm.new(controller)
  controller.set_view(view)
  view.create.show
end

#show_viewObject



39
40
41
# File 'lib/author/controllers/author_list_controller.rb', line 39

def show_view
  @view.create.show
end

#sort(page, per_page, sort_index) ⇒ Object



90
91
92
93
# File 'lib/author/controllers/author_list_controller.rb', line 90

def sort(page, per_page, sort_index)
  @sort_by = @sort_columns[sort_index]
  refresh_data(page, per_page)
end