Class: Spree::CustomerGroup

Inherits:
Object
  • Object
show all
Includes:
SingleStoreResource
Defined in:
app/models/spree/customer_group.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_customers_to(groups, customer_ids) ⇒ Integer

Adds customers to every given group, skipping memberships that already exist, with one lookup for all groups.

Parameters:

Returns:

  • (Integer) —

    number of memberships created



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/spree/customer_group.rb', line 62

def self.add_customers_to(groups, customer_ids)
  customer_ids = Array(customer_ids).compact_blank.map(&:to_s).uniq
  groups = Array(groups)
  return 0 if customer_ids.empty? || groups.empty?

  existing_pairs = Spree::CustomerGroupUser.
                   where(customer_group_id: groups.map(&:id), customer_id: customer_ids).
                   pluck(:customer_group_id, :customer_id).
                   to_set { |group_id, customer_id| [group_id, customer_id.to_s] }

  now = Time.current
  customer_type = Spree.customer_class.to_s
  records_to_insert = groups.flat_map do |group|
    customer_ids.filter_map do |customer_id|
      next if existing_pairs.include?([group.id, customer_id])

      {
        customer_group_id: group.id,
        customer_id: customer_id,
        customer_type: customer_type,
        created_at: now,
        updated_at: now
      }
    end
  end
  return 0 if records_to_insert.empty?

  Spree::CustomerGroupUser.upsert_all(records_to_insert, on_duplicate: :skip)
  touch_customers(records_to_insert.map { |record| record[:customer_id] }.uniq)
  changed_group_ids = records_to_insert.map { |record| record[:customer_group_id] }.to_set
  groups.select { |group| changed_group_ids.include?(group.id) }.each(&:touch)

  records_to_insert.size
end

.remove_customers_from(groups, customer_ids) ⇒ Integer

Removes customers from every given group.

Parameters:

Returns:

  • (Integer) —

    number of memberships removed



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/spree/customer_group.rb', line 101

def self.remove_customers_from(groups, customer_ids)
  customer_ids = Array(customer_ids).compact_blank.map(&:to_s).uniq
  groups = Array(groups)
  return 0 if customer_ids.empty? || groups.empty?

  memberships = Spree::CustomerGroupUser.where(customer_group_id: groups.map(&:id), customer_id: customer_ids)
  changed_group_ids = memberships.distinct.pluck(:customer_group_id).to_set
  return 0 if changed_group_ids.empty?

  deleted_count = memberships.delete_all
  touch_customers(customer_ids)
  groups.select { |group| changed_group_ids.include?(group.id) }.each(&:touch)

  deleted_count
end

Instance Method Details

#add_customers(user_ids) ⇒ Integer

Bulk add customers to the group

Parameters:

  • user_ids (Array) —

    array of user IDs to add

Returns:

  • (Integer) —

    number of customers added



46
47
48
# File 'app/models/spree/customer_group.rb', line 46

def add_customers(user_ids)
  self.class.add_customers_to([self], user_ids)
end

#customers_count ⇒ Object Also known as: users_count

Instance Methods

FIXME: convert this to counter cache



38
39
40
# File 'app/models/spree/customer_group.rb', line 38

def customers_count
  customer_group_users.size
end

#remove_customers(user_ids) ⇒ Integer

Bulk remove customers from the group

Parameters:

  • user_ids (Array) —

    array of user IDs to remove

Returns:

  • (Integer) —

    number of customers removed



53
54
55
# File 'app/models/spree/customer_group.rb', line 53

def remove_customers(user_ids)
  self.class.remove_customers_from([self], user_ids)
end