Class: Agent

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/agent.rb,
app/models2/agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#agent_idObject

Returns the value of attribute agent_id.



57
58
59
# File 'app/models/agent.rb', line 57

def agent_id
  @agent_id
end

Class Method Details

.import_agents(agent_lists) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'app/models/agent.rb', line 220

def self.import_agents(agent_lists)
  agents = []
  agent_lists.each do |agent_list|
    name_and_role = agent_list[:full_name].split('||')
    if agent_list[:agent_identifier].present?
      agent = Agent.find_by(agent_identifier: agent_list[:agent_identifier])
    else
      agents_matched = Agent.where(full_name: name_and_role[0])
      agents_matched = agents_matched.where(place: agent_list[:place]) if agent_list[:place]
      agent = agents_matched.first
    end
    role_type = name_and_role[1].to_s.strip
    unless agent
      agent = Agent.new(
        full_name: name_and_role[0],
        full_name_transcription: agent_list[:full_name_transcription],
        agent_identifier: agent_list[:agent_identifier],
        place: agent_list[:place],
        language_id: 1,
      )
      agent.required_role = Role.find_by(name: 'Guest')
      agent.save
    end
    agents << agent
  end
  agents.uniq!
  agents
end

.new_agents(agents_params) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'app/models/agent.rb', line 253

def self.new_agents(agents_params)
  return [] unless agents_params
  agents = []
  Agent.transaction do
    agents_params.each do |k, v|
      next if v['_destroy'] == '1'

      agent = nil

      if v['agent_id'].present?
        agent = Agent.find(v['agent_id'])
      elsif v['id'].present?
        agent = Agent.find(v['id'])
      end

      if !agent or agent.full_name != v['full_name']
        v.delete('id')
        v.delete('agent_id')
        v.delete('_destroy')
        agent = Agent.create(v)
      end

      agents << agent
    end
  end

  agents
end

Instance Method Details

#agentsObject



249
250
251
# File 'app/models/agent.rb', line 249

def agents
  self.original_agents + self.derived_agents
end

#check_birth_dateObject



134
135
136
137
138
139
140
141
# File 'app/models/agent.rb', line 134

def check_birth_date
  if date_of_birth.present? && date_of_death.present?
    if date_of_birth > date_of_death
      errors.add(:birth_date)
      errors.add(:death_date)
    end
  end
end

#created(work) ⇒ Object



204
205
206
# File 'app/models/agent.rb', line 204

def created(work)
  creates.find_by(work_id: work.id)
end

#creator?(resource) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'app/models/agent.rb', line 196

def creator?(resource)
  resource.creators.include?(self)
end

#dateObject



186
187
188
189
190
191
192
193
194
# File 'app/models/agent.rb', line 186

def date
  if date_of_birth
    if date_of_death
      "#{date_of_birth} - #{date_of_death}"
    else
      "#{date_of_birth} -"
    end
  end
end

#full_name_alternative_without_spaceObject



168
169
170
# File 'app/models/agent.rb', line 168

def full_name_alternative_without_space
  full_name_alternative.to_s.gsub(/\s/, "")
end

#full_name_transcription_without_spaceObject



164
165
166
# File 'app/models/agent.rb', line 164

def full_name_transcription_without_space
  full_name_transcription.to_s.gsub(/\s/, "")
end

#full_name_without_spaceObject

def full_name_generate

# TODO: 日本人以外は?
name = []
name << self.last_name.to_s.strip
name << self.middle_name.to_s.strip unless self.middle_name.blank?
name << self.first_name.to_s.strip
name << self.corporate_name.to_s.strip
name.join(" ").strip

end



153
154
155
156
157
158
159
160
161
162
# File 'app/models/agent.rb', line 153

def full_name_without_space
  full_name.gsub(/[\s,]/, "")
#  # TODO: 日本人以外は?
#  name = []
#  name << self.last_name.to_s.strip
#  name << self.middle_name.to_s.strip
#  name << self.first_name.to_s.strip
#  name << self.corporate_name.to_s.strip
#  name.join("").strip
end

#nameObject



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/agent.rb', line 172

def name
  name = []
  name << full_name.to_s.strip
  name << full_name_transcription.to_s.strip
  name << full_name_alternative.to_s.strip
  name << full_name_without_space
  #name << full_name_transcription_without_space
  #name << full_name_alternative_without_space
  #name << full_name.wakati rescue nil
  #name << full_name_transcription.wakati rescue nil
  #name << full_name_alternative.wakati rescue nil
  name
end

#owned(item) ⇒ Object



216
217
218
# File 'app/models/agent.rb', line 216

def owned(item)
  owns.where(item_id: item.id)
end

#produced(manifestation) ⇒ Object



212
213
214
# File 'app/models/agent.rb', line 212

def produced(manifestation)
  produces.find_by(manifestation_id: manifestation.id)
end

#publisher?(resource) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'app/models/agent.rb', line 200

def publisher?(resource)
  resource.publishers.include?(self)
end

#realized(expression) ⇒ Object



208
209
210
# File 'app/models/agent.rb', line 208

def realized(expression)
  realizes.find_by(expression_id: expression.id)
end

#set_date_of_birthObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/agent.rb', line 97

def set_date_of_birth
  return if birth_date.blank?
  begin
    date = Time.zone.parse(birth_date.to_s)
  rescue ArgumentError
    begin
      date = Time.zone.parse("#{birth_date}-01")
    rescue ArgumentError
      begin
        date = Time.zone.parse("#{birth_date}-01-01")
      rescue
        nil
      end
    end
  end
  self.date_of_birth = date
end

#set_date_of_deathObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/agent.rb', line 115

def set_date_of_death
  return if death_date.blank?
  begin
    date = Time.zone.parse(death_date.to_s)
  rescue ArgumentError
    begin
      date = Time.zone.parse("#{death_date}-01")
    rescue ArgumentError
      begin
        date = Time.zone.parse("#{death_date}-01-01")
      rescue
        nil
      end
    end
  end

  self.date_of_death = date
end

#set_full_nameObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/agent.rb', line 83

def set_full_name
  if full_name.blank?
    if LibraryGroup.site_config.family_name_first
      self.full_name = [last_name, middle_name, first_name].compact.join(" ").to_s.strip
    else
      self.full_name = [first_name, last_name, middle_name].compact.join(" ").to_s.strip
    end
  end
  if full_name_transcription.blank?
    self.full_name_transcription = [last_name_transcription, middle_name_transcription, first_name_transcription].join(" ").to_s.strip
  end
  [full_name, full_name_transcription]
end

#set_role_and_nameObject



78
79
80
81
# File 'app/models/agent.rb', line 78

def set_role_and_name
  self.required_role = Role.find_by(name: 'Librarian') if required_role_id.nil?
  set_full_name
end