Class: Spree::CustomerGroup
- Inherits:
-
Object
- Object
- Spree::CustomerGroup
- Includes:
- SingleStoreResource
- Defined in:
- app/models/spree/customer_group.rb
Class Method Summary collapse
-
.add_customers_to(groups, customer_ids) ⇒ Integer
Adds customers to every given group, skipping memberships that already exist, with one lookup for all groups.
-
.remove_customers_from(groups, customer_ids) ⇒ Integer
Removes customers from every given group.
Instance Method Summary collapse
-
#add_customers(user_ids) ⇒ Integer
Bulk add customers to the group.
-
#customers_count ⇒ Object
(also: #users_count)
Instance Methods.
-
#remove_customers(user_ids) ⇒ Integer
Bulk remove customers from the group.
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.
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.
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
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
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 |