Class: Event

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/event.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.csv_header(role: 'Guest') ⇒ Object

CSVのヘッダ

Parameters:

  • role (String) (defaults to: 'Guest')

    権限



61
62
63
# File 'app/models/event.rb', line 61

def self.csv_header(role: 'Guest')
  Event.new.to_hash(role: role).keys
end

.export(role: 'Guest', col_sep: "\t") ⇒ Object

TSVでのエクスポート

Parameters:

  • role (String) (defaults to: 'Guest')

    権限

  • col_sep (String) (defaults to: "\t")

    区切り文字



87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/event.rb', line 87

def self.export(role: 'Guest', col_sep: "\t")
  file = Tempfile.create('event_export') do |f|
    f.write Event.csv_header(role: role).to_csv(col_sep: col_sep)
    Event.find_each do |event|
      f.write event.to_hash(role: role).values.to_csv(col_sep: col_sep)
    end

    f.rewind
    f.read
  end
end

Instance Method Details

#check_dateObject



46
47
48
49
50
51
52
53
# File 'app/models/event.rb', line 46

def check_date
  if start_at and end_at
    if start_at >= end_at
      errors.add(:start_at)
      errors.add(:end_at)
    end
  end
end

#set_all_dayObject



39
40
41
42
43
44
# File 'app/models/event.rb', line 39

def set_all_day
  if start_at and end_at
    self.start_at = start_at.beginning_of_day
    self.end_at = end_at.end_of_day
  end
end

#set_dateObject



33
34
35
36
37
# File 'app/models/event.rb', line 33

def set_date
  if all_day
    set_all_day
  end
end

#set_display_nameObject



55
56
57
# File 'app/models/event.rb', line 55

def set_display_name
  self.display_name = name if display_name.blank?
end

#to_hash(role: 'Guest') ⇒ Object

CSV出力用のハッシュ

Parameters:

  • role (String) (defaults to: 'Guest')

    権限



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/event.rb', line 67

def to_hash(role: 'Guest')
  record = {
    name: name,
    display_name: display_name,
    event_category: event_category.try(:name),
    library: library.try(:name),
    start_at: start_at,
    end_at: end_at,
    all_day: all_day,
    note: note,
    created_at: created_at,
    updated_at: updated_at
  }

  record
end