Class: ProductFeature

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/product_feature.rb

Overview

create_table :product_features do |t|

t.references :product_feature_type
t.references :product_feature_value

t.timestamps

end

add_index :product_features, :product_feature_type_id, :name => ‘prod_feature_type_idx’ add_index :product_features, :product_feature_value_id, :name => ‘prod_feature_value_idx’

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_create(product_feature_type, product_feature_value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/product_feature.rb', line 22

def self.find_or_create(product_feature_type, product_feature_value)
  product_feature = ProductFeature.where(product_feature_type_id: product_feature_type.id,
                                         product_feature_value_id: product_feature_value.id).first

  unless product_feature
    product_feature = ProductFeature.create(product_feature_type: product_feature_type,
                                            product_feature_value: product_feature_value)
  end

  product_feature
end

.get_feature_types(product_features) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/product_feature.rb', line 34

def self.get_feature_types(product_features)
  array = []
  already_filtered_product_features = if product_features
                                        product_features.map { |pf| pf.product_feature_type }
                                      else
                                        []
                                      end
  ProductFeatureType.each_with_level(ProductFeatureType.root.self_and_descendants) do |o, level|
    if !already_filtered_product_features.include?(o) && level != 0
      array << {feature_type: o, parent_id: o.parent_id, level: level}
    end
  end

  block_given? ? yield(array) : array
end

.get_values(feature_type, product_features = []) ⇒ Object



54
55
56
57
58
59
60
61
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
# File 'app/models/product_feature.rb', line 54

def self.get_values(feature_type, product_features=[])
  feature_value_ids = feature_type.product_feature_values.order('description').pluck(:id)
  valid_feature_value_ids = feature_value_ids.dup

  # if there are a product features passed then it is being scoped by that product feature and
  # we only what valid interactions
  unless product_features.empty?
    product_features.each do |product_feature|

      # check each possible feature type / feature value combination for the given feature_type
      feature_value_ids.each do |value_id|

        # Is there a product feature to support this feature type / feature value combination?
        test_product_feature = ProductFeature.where(product_features: {product_feature_type_id: feature_type.id, product_feature_value_id: value_id}).last

        valid_feature_value_ids.delete_at(valid_feature_value_ids.index(value_id)) unless test_product_feature
        next unless test_product_feature

        unless test_product_feature.find_interactions(:invalid).empty?
          if test_product_feature.find_interactions(:invalid).where('product_feature_to_id = ?', product_feature.id).count == 1
            index = valid_feature_value_ids.index(value_id)
            if index
              valid_feature_value_ids.delete_at(index)
            end
          end
        end

      end # feature_value_ids loop
    end # product_features loop

  end

  valid_feature_value_ids.uniq
end

Instance Method Details

#destroy_interactionsObject

destroy all interactions this ProductFeature is part of



113
114
115
# File 'app/models/product_feature.rb', line 113

def destroy_interactions
  self.interactions.destroy_all
end

#feature_of_recordsObject



50
51
52
# File 'app/models/product_feature.rb', line 50

def feature_of_records
  product_feature_applicabilities.map { |o| o.feature_of_record_type.constantize.find(o.feature_of_record_id) }
end

#find_interactions(type) ⇒ Object

find product feature interactions by ProductFeatureInteractionsType



105
106
107
108
109
110
# File 'app/models/product_feature.rb', line 105

def find_interactions(type)
  # look up type if iid is passed
  type = ProductFeatureInteractionType.iid(type.to_s)

  self.from_interactions.where('product_feature_interaction_type_id' => type.id)
end

#from_interactionsObject



100
101
102
# File 'app/models/product_feature.rb', line 100

def from_interactions
  ProductFeatureInteraction.where('product_feature_from_id = ?', self.id)
end

#interactionsObject

Product Feature Interactions



92
93
94
# File 'app/models/product_feature.rb', line 92

def interactions
  ProductFeatureInteraction.where('product_feature_from_id = ? or product_feature_to_id = ?', self.id, self.id)
end

#to_interactionsObject



96
97
98
# File 'app/models/product_feature.rb', line 96

def to_interactions
  ProductFeatureInteraction.where('product_feature_to_id = ?', self.id)
end