Class: Spree::CustomerGroup
- Inherits:
-
Object
- Object
- Spree::CustomerGroup
- Defined in:
- app/models/spree/customer_group.rb
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.
Instance Method Details
#add_customers(user_ids) ⇒ Integer
Bulk add customers to the group
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/spree/customer_group.rb', line 42 def add_customers(user_ids) return 0 if user_ids.blank? user_ids = Array(user_ids).map(&:to_s).uniq return 0 if user_ids.empty? # Get existing user IDs to avoid duplicates existing_user_ids = customer_group_users.where(user_id: user_ids).pluck(:user_id).map(&:to_s).to_set now = Time.current user_type = Spree.user_class.to_s records_to_insert = user_ids.filter_map do |user_id| next if existing_user_ids.include?(user_id) { customer_group_id: id, user_id: user_id, user_type: user_type, created_at: now, updated_at: now } end return 0 if records_to_insert.empty? Spree::CustomerGroupUser.upsert_all(records_to_insert, on_duplicate: :skip) added_user_ids = records_to_insert.map { |r| r[:user_id] } touch_users(added_user_ids) touch records_to_insert.size end |
#customers_count ⇒ Object Also known as: users_count
Instance Methods
FIXME: convert this to counter cache
34 35 36 |
# File 'app/models/spree/customer_group.rb', line 34 def customers_count customer_group_users.size end |
#remove_customers(user_ids) ⇒ Integer
Bulk remove customers from the group
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/spree/customer_group.rb', line 79 def remove_customers(user_ids) return 0 if user_ids.blank? user_ids = Array(user_ids).map(&:to_s).uniq return 0 if user_ids.empty? deleted_count = customer_group_users.where(user_id: user_ids).delete_all if deleted_count > 0 touch_users(user_ids) touch end deleted_count end |