Skip to content

Commit

Permalink
Flat_map
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz-jakubiak committed Jan 26, 2020
1 parent e759105 commit 488f235
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions core/app/models/spree/calculator/percent_per_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def compute(object = nil)
# Copied from per_item.rb
def matching_products
if compute_on_promotion?
calculable.promotion.rules.map do |rule|
calculable.promotion.rules.flat_map do |rule|
rule.respond_to?(:products) ? rule.products : []
end.flatten
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,11 @@ def run_touch_callbacks

# Iterate through this product's taxons and taxonomies and touch their timestamps in a batch
def touch_taxons
taxons_to_touch = taxons.map(&:self_and_ancestors).flatten.uniq
taxons_to_touch = taxons.flat_map(&:self_and_ancestors).uniq
unless taxons_to_touch.empty?
Spree::Taxon.where(id: taxons_to_touch.map(&:id)).update_all(updated_at: Time.current)

taxonomy_ids_to_touch = taxons_to_touch.map(&:taxonomy_id).flatten.uniq
taxonomy_ids_to_touch = taxons_to_touch.flat_map(&:taxonomy_id).uniq
Spree::Taxonomy.where(id: taxonomy_ids_to_touch).update_all(updated_at: Time.current)
end
end
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/product/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def price_table_name

# specifically avoid having an order for taxon search (conflicts with main order)
def prepare_taxon_conditions(taxons)
ids = taxons.map { |taxon| taxon.self_and_descendants.pluck(:id) }.flatten.uniq
ids = taxons.flat_map { |taxon| taxon.self_and_descendants.pluck(:id) }.uniq
joins(:taxons).where("#{Spree::Taxon.table_name}.id" => ids)
end

Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def eligible_rules(promotable, options = {})
end

def products
rules.where(type: "Spree::Promotion::Rules::Product").map(&:products).flatten.uniq
rules.where(type: "Spree::Promotion::Rules::Product").flat_map(&:products).uniq
end

# Whether the promotion has exceeded it's usage restrictions.
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/shipping_manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def for_order(order)
def items
# Grouping by the ID means that we don't have to call out to the association accessor
# This makes the grouping by faster because it results in less SQL cache hits.
@inventory_units.group_by(&:variant_id).map do |_variant_id, variant_units|
@inventory_units.group_by(&:variant_id).flat_map do |_variant_id, variant_units|
variant_units.group_by(&:line_item_id).map do |_line_item_id, units|
states = {}
units.group_by(&:state).each { |state, iu| states[state] = iu.count }
Expand All @@ -25,6 +25,6 @@ def items

ManifestItem.new(first_unit.line_item, first_unit.variant, units.length, states)
end
end.flatten
end
end
end
4 changes: 2 additions & 2 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ def display_image(fallback: true)
#
# @return [Array<Spree::VariantPropertyRuleValue>] variant_properties
def variant_properties
product.variant_property_rules.map do |rule|
product.variant_property_rules.flat_map do |rule|
rule.values if rule.applies_to_variant?(self)
end.flatten.compact
end.compact
end

# The gallery for the variant, which represents all the images
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/core/product_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def self.brand_filter
brand_property = Spree::Property.find_by(name: 'brand')
brands = brand_property ? Spree::ProductProperty.where(property_id: brand_property.id).pluck(:value).uniq.map(&:to_s) : []
pp = Spree::ProductProperty.arel_table
conds = Hash[*brands.map { |brand| [brand, pp[:value].eq(brand)] }.flatten]
conds = Hash[*brands.flat_map { |brand| [brand, pp[:value].eq(brand)] }]
{
name: 'Brands',
scope: :brand_any,
Expand Down Expand Up @@ -184,7 +184,7 @@ def self.taxons_below(taxon)
# idea: expand the format to allow nesting of labels?
def self.all_taxons
Spree::Deprecation.warn "all_taxons is deprecated in solidus_core. Please add it to your own application to continue using it."
taxons = Spree::Taxonomy.all.map { |element| [element.root] + element.root.descendants }.flatten
taxons = Spree::Taxonomy.all.flat_map { |element| [element.root] + element.root.descendants }
{
name: 'All taxons',
scope: :taxons_id_equals_any,
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/order/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ def assert_state_changed(order, from, to)
it "should not keep old events when checkout_flow is redefined" do
state_machine = Spree::Order.state_machine
expect(state_machine.states.any? { |s| s.name == :address }).to be false
known_states = state_machine.events[:next].branches.map(&:known_states).flatten
known_states = state_machine.events[:next].branches.flat_map(&:known_states)
expect(known_states).not_to include(:address)
expect(known_states).not_to include(:delivery)
expect(known_states).not_to include(:confirm)
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/taxon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
it 'returns all descendant variants' do
variants = taxon.all_variants
expect(variants.count).to eq(9)
expect(variants).to match_array([product1, product2, product3].map{ |p| p.variants_including_master }.flatten)
expect(variants).to match_array([product1, product2, product3].flat_map{ |p| p.variants_including_master })
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions frontend/spec/features/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
within(:css, '#sidebar_products_search') { click_button "Search" }

expect(page.all('ul.product-listing li').size).to eq(3)
tmp = page.all('ul.product-listing li a').map(&:text).flatten.compact
tmp = page.all('ul.product-listing li a').flat_map(&:text).compact
tmp.delete("")
expect(tmp.sort!).to eq(["Ruby on Rails Mug", "Ruby on Rails Stein", "Ruby on Rails Tote"])
end
Expand All @@ -245,7 +245,7 @@
within(:css, '#sidebar_products_search') { click_button "Search" }

expect(page.all('ul.product-listing li').size).to eq(4)
tmp = page.all('ul.product-listing li a').map(&:text).flatten.compact
tmp = page.all('ul.product-listing li a').flat_map(&:text).compact
tmp.delete("")
expect(tmp.sort!).to eq(["Ruby on Rails Bag",
"Ruby on Rails Baseball Jersey",
Expand Down
12 changes: 6 additions & 6 deletions frontend/spec/features/taxons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
within(:css, '#taxonomies') { click_link "Ruby on Rails" }

expect(page.all('ul.product-listing li').size).to eq(7)
tmp = page.all('ul.product-listing li a').map(&:text).flatten.compact
tmp = page.all('ul.product-listing li a').flat_map(&:text).compact
tmp.delete("")
array = ["Ruby on Rails Bag",
"Ruby on Rails Baseball Jersey",
Expand All @@ -89,7 +89,7 @@
within(:css, '#taxonomies') { click_link "Ruby" }

expect(page.all('ul.product-listing li').size).to eq(1)
tmp = page.all('ul.product-listing li a').map(&:text).flatten.compact
tmp = page.all('ul.product-listing li a').flat_map(&:text).compact
tmp.delete("")
expect(tmp.sort!).to eq(["Ruby Baseball Jersey"])
end
Expand All @@ -98,7 +98,7 @@
within(:css, '#taxonomies') { click_link "Apache" }

expect(page.all('ul.product-listing li').size).to eq(1)
tmp = page.all('ul.product-listing li a').map(&:text).flatten.compact
tmp = page.all('ul.product-listing li a').flat_map(&:text).compact
tmp.delete("")
expect(tmp.sort!).to eq(["Apache Baseball Jersey"])
end
Expand All @@ -107,7 +107,7 @@
click_link "Clothing"

expect(page.all('ul.product-listing li').size).to eq(5)
tmp = page.all('ul.product-listing li a').map(&:text).flatten.compact
tmp = page.all('ul.product-listing li a').flat_map(&:text).compact
tmp.delete("")
expect(tmp.sort!).to eq(["Apache Baseball Jersey",
"Ruby Baseball Jersey",
Expand All @@ -120,7 +120,7 @@
click_link "Mugs"

expect(page.all('ul.product-listing li').size).to eq(2)
tmp = page.all('ul.product-listing li a').map(&:text).flatten.compact
tmp = page.all('ul.product-listing li a').flat_map(&:text).compact
tmp.delete("")
expect(tmp.sort!).to eq(["Ruby on Rails Mug", "Ruby on Rails Stein"])
end
Expand All @@ -129,7 +129,7 @@
click_link "Bags"

expect(page.all('ul.product-listing li').size).to eq(2)
tmp = page.all('ul.product-listing li a').map(&:text).flatten.compact
tmp = page.all('ul.product-listing li a').flat_map(&:text).compact
tmp.delete("")
expect(tmp.sort!).to eq(["Ruby on Rails Bag", "Ruby on Rails Tote"])
end
Expand Down

0 comments on commit 488f235

Please sign in to comment.