enju_leaf 1.2.0.beta.1 → 1.2.0.beta.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/lib/enju_leaf/engine.rb +1 -15
  3. data/lib/enju_leaf/version.rb +1 -1
  4. data/lib/generators/enju_leaf/quick_install/quick_install_generator.rb +3 -3
  5. data/lib/generators/enju_leaf/setup/setup_generator.rb +0 -1
  6. data/spec/controllers/page_controller_spec.rb +1 -1
  7. data/spec/dummy/app/controllers/application_controller.rb +0 -1
  8. data/spec/dummy/app/helpers/application_helper.rb +2 -1
  9. data/spec/dummy/app/models/user.rb +0 -1
  10. data/spec/dummy/config/application.rb +1 -2
  11. data/{db → spec/dummy/db}/migrate/041_create_roles.rb +0 -0
  12. data/spec/dummy/db/migrate/059_create_libraries.rb +1 -5
  13. data/spec/dummy/db/migrate/069_create_shelves.rb +1 -5
  14. data/{db → spec/dummy/db}/migrate/077_create_user_groups.rb +0 -0
  15. data/spec/dummy/db/migrate/130_create_request_status_types.rb +1 -5
  16. data/spec/dummy/db/migrate/131_create_request_types.rb +1 -5
  17. data/spec/dummy/db/migrate/20100314190054_add_opening_hour_to_library.rb +9 -0
  18. data/{db → spec/dummy/db}/migrate/20100606065209_create_user_has_roles.rb +0 -0
  19. data/{db → spec/dummy/db}/migrate/20110318183304_add_valid_period_for_new_user_to_user_group.rb +0 -0
  20. data/spec/dummy/db/migrate/20111129044509_add_pickup_location_to_reserve.rb +6 -0
  21. data/{db → spec/dummy/db}/migrate/20130221154434_add_additional_attributes_to_user.rb +0 -0
  22. data/spec/dummy/db/migrate/20130412083556_add_latitude_and_longitude_to_library.rb +6 -0
  23. data/{db → spec/dummy/db}/migrate/20140110122216_create_user_import_files.rb +0 -0
  24. data/{db → spec/dummy/db}/migrate/20140110131010_create_user_import_results.rb +0 -0
  25. data/{db → spec/dummy/db}/migrate/20140122054321_create_profiles.rb +0 -0
  26. data/{db → spec/dummy/db}/migrate/20140524074813_create_user_import_file_transitions.rb +0 -0
  27. data/{db → spec/dummy/db}/migrate/20140610123439_drop_email_unique_constraint_enju_leaf_rc10.rb +0 -0
  28. data/{db → spec/dummy/db}/migrate/20140628072217_add_user_encoding_to_user_import_file.rb +0 -0
  29. data/{db → spec/dummy/db}/migrate/20140709113413_create_user_export_files.rb +0 -0
  30. data/{db → spec/dummy/db}/migrate/20140709113905_create_user_export_file_transitions.rb +0 -0
  31. data/{db → spec/dummy/db}/migrate/20140720170714_add_default_library_id_to_user_import_file.rb +0 -0
  32. data/{db → spec/dummy/db}/migrate/20140720170735_add_default_user_group_id_to_user_import_file.rb +0 -0
  33. data/{db → spec/dummy/db}/migrate/20140811031145_add_expired_at_to_profile.rb +0 -0
  34. data/spec/dummy/db/migrate/20140821151023_create_colors.rb +14 -0
  35. data/{db → spec/dummy/db}/migrate/20141003181336_add_full_name_transcription_to_profile.rb +0 -0
  36. data/{db → spec/dummy/db}/migrate/20141003182825_add_date_of_birth_to_profile.rb +0 -0
  37. data/spec/dummy/db/migrate/20141020120523_add_library_id_to_checkout.rb +6 -0
  38. data/spec/dummy/db/migrate/20150106001709_create_demands.rb +14 -0
  39. data/{db → spec/dummy/db}/migrate/20150421023923_create_identities.rb +0 -0
  40. data/{db → spec/dummy/db}/migrate/20150506105356_add_error_message_to_user_import_result.rb +0 -0
  41. data/spec/dummy/db/migrate/20150924115059_create_withdraws.rb +13 -0
  42. data/{db → spec/dummy/db}/migrate/20151126005552_add_provider_to_identity.rb +0 -0
  43. data/spec/dummy/db/migrate/20160610093229_add_html_snippet_to_library_group.rb +5 -0
  44. data/{db → spec/dummy/db}/migrate/20160627232219_add_most_recent_to_user_import_file_transitions.rb +0 -0
  45. data/{db → spec/dummy/db}/migrate/20160627232316_add_most_recent_to_user_export_file_transitions.rb +0 -0
  46. data/spec/dummy/db/migrate/20160703184619_add_most_recent_to_reserve_transitions.rb +9 -0
  47. data/spec/dummy/db/migrate/20160703184650_add_most_recent_to_manifestation_checkout_stat_transitions.rb +9 -0
  48. data/spec/dummy/db/migrate/20160703184723_add_most_recent_to_manifestation_reserve_stat_transitions.rb +9 -0
  49. data/spec/dummy/db/migrate/20160703184747_add_most_recent_to_user_checkout_stat_transitions.rb +9 -0
  50. data/spec/dummy/db/migrate/20160703184805_add_most_recent_to_user_reserve_stat_transitions.rb +9 -0
  51. data/spec/dummy/db/migrate/20160703185015_add_most_recent_to_message_transitions.rb +9 -0
  52. data/spec/dummy/db/migrate/20160703190209_add_foreign_key_on_manifestation_id_to_reserve.rb +5 -0
  53. data/spec/dummy/db/schema.rb +47 -12
  54. data/spec/fixtures/library_groups.yml +1 -4
  55. data/spec/models/user_spec.rb +0 -22
  56. data/spec/rails_helper.rb +1 -1
  57. data/spec/views/page/index.html.erb_spec.rb +0 -2
  58. metadata +96 -337
  59. data/app/controllers/concerns/enju_leaf/controller.rb +0 -225
  60. data/app/controllers/my_accounts_controller.rb +0 -93
  61. data/app/controllers/names_controller.rb +0 -60
  62. data/app/controllers/profiles_controller.rb +0 -219
  63. data/app/controllers/roles_controller.rb +0 -61
  64. data/app/controllers/user_export_files_controller.rb +0 -116
  65. data/app/controllers/user_groups_controller.rb +0 -118
  66. data/app/controllers/user_import_files_controller.rb +0 -129
  67. data/app/controllers/user_import_results_controller.rb +0 -57
  68. data/app/mailers/notifier.rb +0 -30
  69. data/app/models/identity.rb +0 -9
  70. data/app/models/profile.rb +0 -101
  71. data/app/models/role.rb +0 -50
  72. data/app/models/user_export_file.rb +0 -67
  73. data/app/models/user_export_file_state_machine.rb +0 -15
  74. data/app/models/user_export_file_transition.rb +0 -20
  75. data/app/models/user_group.rb +0 -29
  76. data/app/models/user_has_role.rb +0 -19
  77. data/app/models/user_import_file.rb +0 -352
  78. data/app/models/user_import_file_state_machine.rb +0 -19
  79. data/app/models/user_import_file_transition.rb +0 -20
  80. data/app/models/user_import_result.rb +0 -20
  81. data/app/policies/application_policy.rb +0 -53
  82. data/app/policies/name_policy.rb +0 -21
  83. data/app/policies/profile_policy.rb +0 -55
  84. data/app/policies/role_policy.rb +0 -21
  85. data/app/policies/user_export_file_policy.rb +0 -21
  86. data/app/policies/user_group_policy.rb +0 -22
  87. data/app/policies/user_import_file_policy.rb +0 -21
  88. data/app/policies/user_import_result_policy.rb +0 -21
  89. data/app/views/my_accounts/_edit_credential.html.erb +0 -99
  90. data/app/views/my_accounts/_form.html.erb +0 -24
  91. data/app/views/my_accounts/_show.html.erb +0 -1
  92. data/app/views/my_accounts/edit.html.erb +0 -32
  93. data/app/views/my_accounts/show.html+phone.erb +0 -1
  94. data/app/views/my_accounts/show.html.erb +0 -49
  95. data/app/views/names/_form.html.erb +0 -41
  96. data/app/views/names/edit.html.erb +0 -6
  97. data/app/views/names/index.html.erb +0 -37
  98. data/app/views/names/new.html.erb +0 -5
  99. data/app/views/names/show.html.erb +0 -34
  100. data/app/views/profiles/_edit_credential.html.erb +0 -88
  101. data/app/views/profiles/_edit_profile.html.erb +0 -42
  102. data/app/views/profiles/_form.html.erb +0 -15
  103. data/app/views/profiles/_show.html.erb +0 -1
  104. data/app/views/profiles/edit.html.erb +0 -33
  105. data/app/views/profiles/index.html.erb +0 -70
  106. data/app/views/profiles/new.html.erb +0 -88
  107. data/app/views/profiles/show.html+phone.erb +0 -1
  108. data/app/views/profiles/show.html.erb +0 -71
  109. data/app/views/roles/_form.html.erb +0 -19
  110. data/app/views/roles/edit.html.erb +0 -13
  111. data/app/views/roles/index.html.erb +0 -32
  112. data/app/views/roles/new.html.erb +0 -12
  113. data/app/views/roles/show.html.erb +0 -46
  114. data/app/views/user_export_files/_form.html.erb +0 -12
  115. data/app/views/user_export_files/edit.html.erb +0 -6
  116. data/app/views/user_export_files/index.html.erb +0 -44
  117. data/app/views/user_export_files/new.html.erb +0 -16
  118. data/app/views/user_export_files/show.html.erb +0 -36
  119. data/app/views/user_groups/_form.html.erb +0 -40
  120. data/app/views/user_groups/edit.html.erb +0 -13
  121. data/app/views/user_groups/index.html.erb +0 -51
  122. data/app/views/user_groups/new.html.erb +0 -12
  123. data/app/views/user_groups/show.html.erb +0 -68
  124. data/app/views/user_import_files/_form.html.erb +0 -61
  125. data/app/views/user_import_files/_results.html.erb +0 -22
  126. data/app/views/user_import_files/edit.html.erb +0 -6
  127. data/app/views/user_import_files/index.html.erb +0 -45
  128. data/app/views/user_import_files/new.html.erb +0 -60
  129. data/app/views/user_import_files/show.html.erb +0 -85
  130. data/app/views/user_import_results/_form.html.erb +0 -29
  131. data/app/views/user_import_results/_list.html.erb +0 -22
  132. data/app/views/user_import_results/_list_lines.html.erb +0 -24
  133. data/app/views/user_import_results/index.html.erb +0 -25
  134. data/app/views/user_import_results/index.txt.erb +0 -3
  135. data/app/views/user_import_results/show.html.erb +0 -33
  136. data/db/migrate/20091202124834_create_versions.rb +0 -18
  137. data/spec/controllers/my_accounts_controller_spec.rb +0 -229
  138. data/spec/controllers/profiles_controller_spec.rb +0 -594
  139. data/spec/controllers/roles_controller_spec.rb +0 -136
  140. data/spec/controllers/user_export_files_controller_spec.rb +0 -296
  141. data/spec/controllers/user_groups_controller_spec.rb +0 -443
  142. data/spec/controllers/user_import_files_controller_spec.rb +0 -305
  143. data/spec/controllers/user_import_results_controller_spec.rb +0 -157
  144. data/spec/fixtures/user_export_files.yml +0 -25
  145. data/spec/fixtures/user_import_files.yml +0 -51
  146. data/spec/fixtures/user_import_results.yml +0 -26
  147. data/spec/fixtures/user_reserve_stats.yml +0 -27
  148. data/spec/models/basket_spec.rb +0 -23
  149. data/spec/models/profile_spec.rb +0 -98
  150. data/spec/models/role_spec.rb +0 -40
  151. data/spec/models/user_export_file_spec.rb +0 -30
  152. data/spec/models/user_group_spec.rb +0 -37
  153. data/spec/models/user_has_role_spec.rb +0 -18
  154. data/spec/models/user_import_file_spec.rb +0 -220
  155. data/spec/models/user_import_result_spec.rb +0 -20
  156. data/spec/requests/profiles_spec.rb +0 -9
  157. data/spec/routing/profiles_routing_spec.rb +0 -35
  158. data/spec/routing/resource_import_files_routing_spec.rb +0 -35
  159. data/spec/routing/roles_routing_spec.rb +0 -35
  160. data/spec/routing/users_spec.rb +0 -14
  161. data/spec/views/my_accounts/show.html.erb_spec.rb +0 -43
  162. data/spec/views/profiles/edit.html.erb_spec.rb +0 -42
  163. data/spec/views/profiles/index.html.erb_spec.rb +0 -19
  164. data/spec/views/profiles/new.html.erb_spec.rb +0 -25
  165. data/spec/views/profiles/show.html.erb_spec.rb +0 -31
  166. data/spec/views/user_export_files/new.html.erb_spec.rb +0 -19
  167. data/spec/views/user_import_results/index.html.erb_spec.rb +0 -29
@@ -1,30 +0,0 @@
1
- class Notifier < ActionMailer::Base
2
- def message_notification(message_id)
3
- message = Message.find(message_id)
4
- I18n.locale = message.receiver.profile.locale.try(:to_sym) || I18n.default_locale
5
- from = "#{LibraryGroup.system_name(message.receiver.profile.locale)} <#{LibraryGroup.site_config.user.email}>"
6
- if message.subject
7
- subject = message.subject
8
- else
9
- subject = I18n.t('message.new_message_from_library', library: LibraryGroup.system_name(message.receiver.user.profile.locale))
10
- end
11
- if message.sender
12
- @sender_name = message.sender.username
13
- else
14
- @sender_name = LibraryGroup.system_name(message.receiver.profile.locale)
15
- end
16
- @message = message
17
- @locale = message.receiver.profile.locale
18
- mail(from: from, to: message.receiver.email, subject: subject)
19
- end
20
-
21
- def manifestation_info(user_id, manifestation_id)
22
- user = User.find(user_id)
23
- manifestation = Manifestation.find(manifestation_id)
24
- from = "#{LibraryGroup.system_name(user.profile.locale)} <#{LibraryGroup.site_config.user.email}>"
25
- subject = "#{manifestation.original_title} : #{LibraryGroup.system_name(user.profile.locale)}"
26
- @user = user
27
- @manifestation = manifestation
28
- mail(from: from, to: user.email, subject: subject)
29
- end
30
- end
@@ -1,9 +0,0 @@
1
- class Identity < ActiveRecord::Base
2
- belongs_to :profile
3
- validates :name, presence: true, uniqueness: {scope: :provider}
4
- validates :provider, presence: true
5
-
6
- def self.find_for_oauth(auth)
7
- where(name: auth.uid, provider: auth.provider).first_or_create
8
- end
9
- end
@@ -1,101 +0,0 @@
1
- class Profile < ActiveRecord::Base
2
- enju_search_log_profile_model if defined?(EnjuSearchLog)
3
-
4
- scope :administrators, -> { joins(user: :role).where('roles.name = ?', 'Administrator') }
5
- scope :librarians, -> { joins(user: :role).where('roles.name = ? OR roles.name = ?', 'Administrator', 'Librarian') }
6
- belongs_to :user, dependent: :destroy
7
- belongs_to :library, validate: true
8
- belongs_to :user_group
9
- belongs_to :required_role, class_name: 'Role', foreign_key: 'required_role_id' #, validate: true
10
- has_many :identities
11
- has_many :agents
12
- accepts_nested_attributes_for :identities, allow_destroy: true, reject_if: :all_blank
13
-
14
- validates_associated :user_group, :library
15
- validates_associated :user
16
- validates_presence_of :user_group, :library, :locale #, :user_number
17
- validates :user_number, uniqueness: true, format: { with: /\A[0-9A-Za-z_]+\z/ }, allow_blank: true
18
- validates :user_id, uniqueness: true, allow_blank: true
19
- validates :birth_date, format: { with: /\A\d{4}-\d{1,2}-\d{1,2}\z/ }, allow_blank: true
20
-
21
- strip_attributes only: :user_number
22
-
23
- attr_accessor :birth_date
24
-
25
- searchable do
26
- text :user_number, :full_name, :full_name_transcription, :note
27
- string :user_number
28
- text :username do
29
- user.try(:username)
30
- end
31
- text :email do
32
- user.try(:email)
33
- end
34
- string :username do
35
- user.try(:username)
36
- end
37
- string :email do
38
- user.try(:email)
39
- end
40
- time :created_at
41
- time :updated_at
42
- boolean :active do
43
- user.try(:active_for_authentication?)
44
- end
45
- integer :required_role_id
46
- end
47
-
48
- before_validation :set_role_and_agent, on: :create
49
- before_save :set_expired_at, :set_date_of_birth
50
- accepts_nested_attributes_for :user
51
-
52
- # 既定のユーザ権限を設定します。
53
- # @return [void]
54
- def set_role_and_agent
55
- self.required_role = Role.where(name: 'Librarian').first unless required_role
56
- self.locale = I18n.default_locale.to_s unless locale
57
- end
58
-
59
- # ユーザの有効期限を設定します。
60
- # @return [Time]
61
- def set_expired_at
62
- if expired_at.blank?
63
- if user_group.valid_period_for_new_user > 0
64
- self.expired_at = user_group.valid_period_for_new_user.days.from_now.end_of_day
65
- end
66
- end
67
- end
68
-
69
- # ユーザの誕生日を設定します。
70
- # @return [Time]
71
- def set_date_of_birth
72
- self.date_of_birth = Time.zone.parse(birth_date) if birth_date
73
- rescue ArgumentError
74
- nil
75
- end
76
- end
77
-
78
- # == Schema Information
79
- #
80
- # Table name: profiles
81
- #
82
- # id :integer not null, primary key
83
- # user_id :integer
84
- # user_group_id :integer
85
- # library_id :integer
86
- # locale :string
87
- # user_number :string
88
- # full_name :text
89
- # note :text
90
- # keyword_list :text
91
- # required_role_id :integer
92
- # created_at :datetime
93
- # updated_at :datetime
94
- # checkout_icalendar_token :string
95
- # save_checkout_history :boolean default(FALSE), not null
96
- # expired_at :datetime
97
- # save_search_history :boolean
98
- # share_bookmarks :boolean
99
- # full_name_transcription :text
100
- # date_of_birth :datetime
101
- #
@@ -1,50 +0,0 @@
1
- class Role < ActiveRecord::Base
2
- include MasterModel
3
- validates :name, presence: true, format: { with: /\A[A-Za-z][a-z_,]*[a-z]\z/ }
4
- has_many :user_has_roles
5
- has_many :users, through: :user_has_roles
6
- after_save :clear_all_cache
7
- after_destroy :clear_all_cache
8
-
9
- extend FriendlyId
10
- friendly_id :name
11
-
12
- def localized_name
13
- display_name.localize
14
- end
15
-
16
- def self.all_cache
17
- if Rails.env == 'production'
18
- Rails.cache.fetch('role_all'){ Role.select(:name).all }
19
- else
20
- Role.select(:name)
21
- end
22
- end
23
-
24
- def clear_all_cache
25
- Rails.cache.delete('role_all')
26
- end
27
-
28
- def self.default_role
29
- Role.where(name: 'Guest').first
30
- end
31
-
32
- private
33
- def valid_name?
34
- true
35
- end
36
- end
37
-
38
- # == Schema Information
39
- #
40
- # Table name: roles
41
- #
42
- # id :integer not null, primary key
43
- # name :string not null
44
- # display_name :string
45
- # note :text
46
- # created_at :datetime
47
- # updated_at :datetime
48
- # score :integer default(0), not null
49
- # position :integer
50
- #
@@ -1,67 +0,0 @@
1
- class UserExportFile < ActiveRecord::Base
2
- include Statesman::Adapters::ActiveRecordQueries
3
- include ExportFile
4
-
5
- if ENV['ENJU_STORAGE'] == 's3'
6
- has_attached_file :user_export, storage: :s3,
7
- s3_credentials: {
8
- access_key: ENV['AWS_ACCESS_KEY_ID'],
9
- secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
10
- bucket: ENV['S3_BUCKET_NAME'],
11
- s3_host_name: ENV['S3_HOST_NAME']
12
- },
13
- s3_permissions: :private
14
- else
15
- has_attached_file :user_export,
16
- path: ":rails_root/private/system/:class/:attachment/:id_partition/:style/:filename"
17
- end
18
- validates_attachment_content_type :user_export, content_type: /\Atext\/plain\Z/
19
-
20
- has_many :user_export_file_transitions
21
-
22
- def state_machine
23
- UserExportFileStateMachine.new(self, transition_class: UserExportFileTransition)
24
- end
25
-
26
- delegate :can_transition_to?, :transition_to!, :transition_to, :current_state,
27
- to: :state_machine
28
-
29
- # エクスポートの処理を実行します。
30
- def export!
31
- transition_to!(:started)
32
- tempfile = Tempfile.new(['user_export_file_', '.txt'])
33
- file = User.export(format: :txt)
34
- tempfile.puts(file)
35
- tempfile.close
36
- self.user_export = File.new(tempfile.path, 'r')
37
- if save
38
- send_message
39
- end
40
- transition_to!(:completed)
41
- rescue => e
42
- transition_to!(:failed)
43
- raise e
44
- end
45
-
46
- def self.transition_class
47
- UserExportFileTransition
48
- end
49
-
50
- def self.initial_state
51
- :pending
52
- end
53
- end
54
-
55
- # == Schema Information
56
- #
57
- # Table name: user_export_files
58
- #
59
- # id :integer not null, primary key
60
- # user_id :integer
61
- # executed_at :datetime
62
- # created_at :datetime
63
- # updated_at :datetime
64
- # user_export_id :string
65
- # user_export_size :integer
66
- # user_import_filename :string
67
- #
@@ -1,15 +0,0 @@
1
- class UserExportFileStateMachine
2
- include Statesman::Machine
3
-
4
- state :pending, initial: true
5
- state :started
6
- state :completed
7
- state :failed
8
-
9
- transition from: :pending, to: :started
10
- transition from: :started, to: [:completed, :failed]
11
-
12
- after_transition(from: :pending, to: :started) do |user_export_file|
13
- user_export_file.update_column(:executed_at, Time.zone.now)
14
- end
15
- end
@@ -1,20 +0,0 @@
1
- class UserExportFileTransition < ActiveRecord::Base
2
- include Statesman::Adapters::ActiveRecordTransition
3
-
4
-
5
- belongs_to :user_export_file, inverse_of: :user_export_file_transitions
6
- #attr_accessible :to_state, :sort_key, :metadata
7
- end
8
-
9
- # == Schema Information
10
- #
11
- # Table name: user_export_file_transitions
12
- #
13
- # id :integer not null, primary key
14
- # to_state :string
15
- # metadata :text default({})
16
- # sort_key :integer
17
- # user_export_file_id :integer
18
- # created_at :datetime
19
- # updated_at :datetime
20
- #
@@ -1,29 +0,0 @@
1
- class UserGroup < ActiveRecord::Base
2
- include MasterModel
3
- has_many :profiles
4
-
5
- validates_numericality_of :valid_period_for_new_user,
6
- greater_than_or_equal_to: 0,
7
- allow_blank: true
8
-
9
- paginates_per 10
10
- end
11
-
12
- # == Schema Information
13
- #
14
- # Table name: user_groups
15
- #
16
- # id :integer not null, primary key
17
- # name :string
18
- # display_name :text
19
- # note :text
20
- # position :integer
21
- # created_at :datetime
22
- # updated_at :datetime
23
- # deleted_at :datetime
24
- # valid_period_for_new_user :integer default(0), not null
25
- # expired_at :datetime
26
- # number_of_day_to_notify_overdue :integer default(1), not null
27
- # number_of_day_to_notify_due_date :integer default(7), not null
28
- # number_of_time_to_notify_overdue :integer default(3), not null
29
- #
@@ -1,19 +0,0 @@
1
- class UserHasRole < ActiveRecord::Base
2
- belongs_to :user
3
- belongs_to :role
4
- accepts_nested_attributes_for :role
5
-
6
- # validates_uniqueness_of :role_id, scope: :user_id
7
- # validates_presence_of :role_id, :user_id
8
- end
9
-
10
- # == Schema Information
11
- #
12
- # Table name: user_has_roles
13
- #
14
- # id :integer not null, primary key
15
- # user_id :integer
16
- # role_id :integer
17
- # created_at :datetime
18
- # updated_at :datetime
19
- #
@@ -1,352 +0,0 @@
1
- class UserImportFile < ActiveRecord::Base
2
- include Statesman::Adapters::ActiveRecordQueries
3
- include ImportFile
4
- default_scope {order('user_import_files.id DESC')}
5
- scope :not_imported, -> { in_state(:pending) }
6
- scope :stucked, -> { in_state(:pending).where('user_import_files.created_at < ?', 1.hour.ago) }
7
-
8
- if ENV['ENJU_STORAGE'] == 's3'
9
- has_attached_file :user_import, storage: :s3,
10
- s3_credentials: {
11
- access_key: ENV['AWS_ACCESS_KEY_ID'],
12
- secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
13
- bucket: ENV['S3_BUCKET_NAME'],
14
- s3_host_name: ENV['S3_HOST_NAME']
15
- },
16
- s3_permissions: :private
17
- else
18
- has_attached_file :user_import,
19
- path: ":rails_root/private/system/:class/:attachment/:id_partition/:style/:filename"
20
- end
21
- validates_attachment_content_type :user_import, content_type: [
22
- 'text/csv',
23
- 'text/plain',
24
- 'text/tab-separated-values',
25
- 'application/octet-stream',
26
- 'application/vnd.ms-excel'
27
- ]
28
- validates_attachment_presence :user_import
29
- belongs_to :user, validate: true
30
- belongs_to :default_user_group, class_name: 'UserGroup'
31
- belongs_to :default_library, class_name: 'Library'
32
- has_many :user_import_results
33
-
34
- has_many :user_import_file_transitions
35
-
36
- attr_accessor :mode
37
-
38
- def state_machine
39
- UserImportFileStateMachine.new(self, transition_class: UserImportFileTransition)
40
- end
41
-
42
- delegate :can_transition_to?, :transition_to!, :transition_to, :current_state,
43
- to: :state_machine
44
-
45
- # 利用者情報をTSVファイルを用いて作成します。
46
- def import
47
- transition_to!(:started)
48
- num = { user_imported: 0, user_found: 0, failed: 0, error: 0 }
49
- rows = open_import_file(create_import_temp_file(user_import))
50
- row_num = 1
51
-
52
- field = rows.first
53
- if [field['username']].reject{ |f| f.to_s.strip == "" }.empty?
54
- raise "username column is not found"
55
- end
56
-
57
- rows.each do |row|
58
- row_num += 1
59
- import_result = UserImportResult.create!(
60
- user_import_file_id: id, body: row.fields.join("\t")
61
- )
62
- next if row['dummy'].to_s.strip.present?
63
-
64
- username = row['username']
65
- new_user = User.where(username: username).first
66
- if new_user
67
- import_result.user = new_user
68
- import_result.save
69
- num[:user_found] += 1
70
- else
71
- new_user = User.new
72
- new_user.role = Role.where(name: row['role']).first
73
- if new_user.role
74
- unless user.has_role?(new_user.role.name)
75
- num[:failed] += 1
76
- next
77
- end
78
- else
79
- new_user.role = Role.find(2) # User
80
- end
81
- new_user.username = username
82
- new_user.assign_attributes(set_user_params(row))
83
- profile = Profile.new
84
- profile.assign_attributes(set_profile_params(row))
85
-
86
- Profile.transaction do
87
- if new_user.valid? and profile.valid?
88
- new_user.profile = profile
89
- import_result.user = new_user
90
- import_result.save!
91
- num[:user_imported] += 1
92
- else
93
- error_message = "line #{row_num}: "
94
- error_message += new_user.errors.full_messages.join(" ")
95
- error_message += profile.errors.full_messages.join(" ")
96
- import_result.error_message = error_message
97
- import_result.save
98
- num[:error] += 1
99
- end
100
- end
101
- end
102
- end
103
-
104
- Sunspot.commit
105
- rows.close
106
- error_messages = user_import_results.order(:id).pluck(:error_message).compact
107
- unless error_messages.empty?
108
- self.error_message = '' if error_message.nil?
109
- self.error_message += "\n"
110
- self.error_message += error_messages.join("\n")
111
- end
112
- save
113
- if num[:error] >= 1
114
- transition_to!(:failed)
115
- else
116
- transition_to!(:completed)
117
- end
118
- send_message
119
- num
120
- rescue => e
121
- transition_to!(:failed)
122
- raise e
123
- end
124
-
125
- # 利用者情報をTSVファイルを用いて更新します。
126
- def modify
127
- transition_to!(:started)
128
- num = { user_updated: 0, user_not_found: 0, failed: 0 }
129
- rows = open_import_file(create_import_temp_file(user_import))
130
- row_num = 1
131
-
132
- field = rows.first
133
- if [field['username']].reject{|f| f.to_s.strip == ""}.empty?
134
- raise "username column is not found"
135
- end
136
-
137
- rows.each do |row|
138
- row_num += 1
139
- next if row['dummy'].to_s.strip.present?
140
- import_result = UserImportResult.create!(
141
- user_import_file_id: id, body: row.fields.join("\t")
142
- )
143
-
144
- username = row['username']
145
- new_user = User.where(username: username).first
146
- if new_user.try(:profile)
147
- new_user.assign_attributes(set_user_params(row))
148
- new_user.profile.assign_attributes(set_profile_params(row))
149
- Profile.transaction do
150
- if new_user.save and new_user.profile.save
151
- num[:user_updated] += 1
152
- import_result.user = new_user
153
- import_result.save!
154
- else
155
- num[:failed] += 1
156
- end
157
- end
158
- else
159
- num[:user_not_found] += 1
160
- end
161
- end
162
-
163
- rows.close
164
- transition_to!(:completed)
165
- Sunspot.commit
166
- send_message
167
- num
168
- rescue => e
169
- self.error_message = "line #{row_num}: #{e.message}"
170
- save
171
- transition_to!(:failed)
172
- raise e
173
- end
174
-
175
- # 利用者情報をTSVファイルを用いて削除します。
176
- def remove
177
- transition_to!(:started)
178
- row_num = 1
179
- rows = open_import_file(create_import_temp_file(user_import))
180
-
181
- field = rows.first
182
- if [field['username']].reject{ |f| f.to_s.strip == "" }.empty?
183
- raise "username column is not found"
184
- end
185
-
186
- rows.each do |row|
187
- row_num += 1
188
- username = row['username'].to_s.strip
189
- remove_user = User.where(username: username).first
190
- if remove_user.try(:deletable_by?, user)
191
- UserImportFile.transaction do
192
- remove_user.destroy
193
- remove_user.profile.destroy
194
- end
195
- end
196
- end
197
- transition_to!(:completed)
198
- send_message
199
- rescue => e
200
- self.error_message = "line #{row_num}: #{e.message}"
201
- save
202
- transition_to!(:failed)
203
- raise e
204
- end
205
-
206
- private
207
- def self.transition_class
208
- UserImportFileTransition
209
- end
210
-
211
- def self.initial_state
212
- :pending
213
- end
214
-
215
- # インポート作業用のファイルを読み込みます。
216
- # @param [File] tempfile 作業用のファイル
217
- def open_import_file(tempfile)
218
- file = CSV.open(tempfile.path, 'r:utf-8', col_sep: "\t")
219
- header_columns = %w(
220
- username role email password user_group user_number expired_at
221
- full_name full_name_transcription required_role locked
222
- keyword_list note locale library dummy
223
- )
224
- if defined?(EnjuCirculation)
225
- header_columns += %w(checkout_icalendar_token save_checkout_history)
226
- end
227
- if defined?(EnjuSearchLog)
228
- header_columns += %w(save_search_history)
229
- end
230
- if defined?(EnjuBookmark)
231
- header_columns += %w(share_bookmarks)
232
- end
233
-
234
- header = file.first
235
- ignored_columns = header - header_columns
236
- unless ignored_columns.empty?
237
- self.error_message = I18n.t('import.following_column_were_ignored', column: ignored_columns.join(', '))
238
- save!
239
- end
240
- rows = CSV.open(tempfile.path, 'r:utf-8', headers: header, col_sep: "\t")
241
- UserImportResult.create!(user_import_file_id: id, body: header.join("\t"))
242
- tempfile.close(true)
243
- file.close
244
- rows
245
- end
246
-
247
- # 未処理のインポート作業用のファイルを一括で処理します。
248
- def self.import
249
- UserImportFile.not_imported.each do |file|
250
- file.import_start
251
- end
252
- rescue
253
- Rails.logger.info "#{Time.zone.now} importing resources failed!"
254
- end
255
-
256
- private
257
- # ユーザ情報のパラメータを設定します。
258
- # @param [Hash] row 利用者情報のハッシュ
259
- def set_user_params(row)
260
- params = {}
261
- params[:email] = row['email'] if row['email'].present?
262
-
263
- if row['password'].present?
264
- params[:password] = row['password']
265
- else
266
- params[:password] = Devise.friendly_token[0..7]
267
- end
268
-
269
- if %w(t true).include?(row['locked'].to_s.downcase.strip)
270
- params[:locked] = '1'
271
- end
272
-
273
- params
274
- end
275
-
276
- # 利用者情報のパラメータを設定します。
277
- # @param [Hash] row 利用者情報のハッシュ
278
- def set_profile_params(row)
279
- params = {}
280
- user_group = UserGroup.where(name: row['user_group']).first
281
- unless user_group
282
- user_group = default_user_group
283
- end
284
- params[:user_group_id] = user_group.id if user_group
285
-
286
- required_role = Role.where(name: row['required_role']).first
287
- unless required_role
288
- required_role = Role.where(name: 'Librarian').first
289
- end
290
- params[:required_role_id] = required_role.id if required_role
291
-
292
- params[:user_number] = row['user_number'] if row['user_number']
293
- params[:full_name] = row['full_name'] if row['full_name']
294
- params[:full_name_transcription] = row['full_name_transcription'] if row['full_name_transcription']
295
-
296
- if row['expired_at'].present?
297
- params[:expired_at] = Time.zone.parse(row['expired_at']).end_of_day
298
- end
299
-
300
- if row['keyword_list'].present?
301
- params[:keyword_list] = row['keyword_list'].split('//').join("\n")
302
- end
303
-
304
- params[:note] = row['note'] if row['note']
305
-
306
- if I18n.available_locales.include?(row['locale'].to_s.to_sym)
307
- params[:locale] = row['locale']
308
- end
309
-
310
- library = Library.where(name: row['library'].to_s.strip).first
311
- unless library
312
- library = default_library || Library.web
313
- end
314
- params[:library_id] = library.id if library
315
-
316
- if defined?(EnjuCirculation)
317
- params[:checkout_icalendar_token] = row['checkout_icalendar_token'] if row['checkout_icalendar_token'].present?
318
- params[:save_checkout_history] = row['save_checkout_history'] if row['save_checkout_history'].present?
319
- end
320
- if defined?(EnjuSearchLog)
321
- params[:save_search_history] = row['save_search_history'] if row['save_search_history'].present?
322
- end
323
- if defined?(EnjuBookmark)
324
- params[:share_bookmarks] = row['share_bookmarks'] if row['share_bookmarks'].present?
325
- end
326
- params
327
- end
328
- end
329
-
330
- # == Schema Information
331
- #
332
- # Table name: user_import_files
333
- #
334
- # id :integer not null, primary key
335
- # user_id :integer
336
- # note :text
337
- # executed_at :datetime
338
- # user_import_file_name :string
339
- # user_import_content_type :string
340
- # user_import_file_size :integer
341
- # user_import_updated_at :datetime
342
- # user_import_fingerprint :string
343
- # edit_mode :string
344
- # error_message :text
345
- # created_at :datetime
346
- # updated_at :datetime
347
- # user_encoding :string
348
- # default_library_id :integer
349
- # default_user_group_id :integer
350
- # user_import_id :string
351
- # user_import_size :integer
352
- #