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.
-
#remove_customers(user_ids) ⇒ Integer
Bulk remove customers from the group.
-
#users_count ⇒ Object
Instance Methods.
Instance Method Details
#add_customers(user_ids) ⇒ Integer
Bulk add customers to the group
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/spree/customer_group.rb', line 33 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 |
#remove_customers(user_ids) ⇒ Integer
Bulk remove customers from the group
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/models/spree/customer_group.rb', line 70 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 |
#users_count ⇒ Object
Instance Methods
26 27 28 |
# File 'app/models/spree/customer_group.rb', line 26 def users_count customer_group_users.count end |