Class: List

Inherits:
ApplicationRecord show all
Includes:
Importable
Defined in:
app/models/list.rb

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.destroyable_typesObject


29
30
31
# File 'app/models/list.rb', line 29

def destroyable_types
  [:label]
end

.movable_typesObject


33
34
35
# File 'app/models/list.rb', line 33

def movable_types
  [:label]
end

.preload_preferences_for_user(lists, user) ⇒ Object


37
38
39
40
41
# File 'app/models/list.rb', line 37

def preload_preferences_for_user(lists, user)
  return unless user

  lists.each { |list| list.preferences_for(user) }
end

Instance Method Details

#as_json(options = {}) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/list.rb', line 81

def as_json(options = {})
  super(options).tap do |json|
    json[:collapsed] = false

    if options.key?(:collapsed)
      json[:collapsed] = collapsed?(options[:current_user])
    end

    if options.key?(:label)
      json[:label] = label.as_json(
        project: board.project,
        only: [:id, :title, :description, :color],
        methods: [:text_color]
      )
    end
  end
end

#collapsed?(user) ⇒ Boolean

Returns:

  • (Boolean)

75
76
77
78
79
# File 'app/models/list.rb', line 75

def collapsed?(user)
  preferences = preferences_for(user)

  preferences.collapsed?
end

#destroyable?Boolean

Returns:

  • (Boolean)

63
64
65
# File 'app/models/list.rb', line 63

def destroyable?
  self.class.destroyable_types.include?(list_type&.to_sym)
end

#movable?Boolean

Returns:

  • (Boolean)

67
68
69
# File 'app/models/list.rb', line 67

def movable?
  self.class.movable_types.include?(list_type&.to_sym)
end

#preferences_for(user) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/list.rb', line 44

def preferences_for(user)
  return preferences.build unless user

  BatchLoader.for(list_id: id, user_id: user.id).batch(default_value: preferences.build(user: user)) do |items, loader|
    list_ids = items.map { |i| i[:list_id] }
    user_ids = items.map { |i| i[:user_id] }

    ListUserPreference.where(list_id: list_ids.uniq, user_id: user_ids.uniq).find_each do |preference|
      loader.call({ list_id: preference.list_id, user_id: preference.user_id }, preference)
    end
  end
end

#titleObject


71
72
73
# File 'app/models/list.rb', line 71

def title
  label? ? label.name : list_type.humanize
end

#update_preferences_for(user, preferences = {}) ⇒ Object


57
58
59
60
61
# File 'app/models/list.rb', line 57

def update_preferences_for(user, preferences = {})
  return unless user

  preferences_for(user).update(preferences)
end