Module: ActsAsFeatureable::FeatureAdditions

Included in:
Feature
Defined in:
lib/acts_as_featureable/feature_additions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(feature_model) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
# File 'lib/acts_as_featureable/feature_additions.rb', line 3

def self.included(feature_model)
  feature_model.belongs_to :featureable, polymorphic: true

  feature_model.scope :ordered, ->{ feature_model.order('position ASC') }

  feature_model.before_validation :assign_title, :assign_summary, :assign_position

  feature_model.validate :feature_limit_not_reached, :ensure_categories

  feature_model.validates_presence_of :featureable_id, :featureable_type

  feature_model.validates_numericality_of :position

  private

  def ensure_categories
    cats = ::ActsAsFeatureable::categories
    # Allow all categories if set to false
    if cats && self.category && !cats.include?(self.category.to_sym)
      errors.add(:category, " is not in the list [#{cats.join(',')}]")
    end
  end

  def feature_limit_not_reached
    limit = ActsAsFeatureable.feature_limit
    errors.add(:base,
      "The feature limit of #{limit} has been reached. Please delete or change an existing feature.") if
      ActsAsFeatureable.categories ? Feature.where(category: self.category).count >= limit : Feature.count >= limit
  end

  def assign_title
    # If there is no title given, check the model.
    # Will check for the attributes in ActsAsFeatureable.auto_title_assign_list.
    unless self.title
      ActsAsFeatureable.auto_title_assign_list.each do |attr|
        if featureable.respond_to?(attr)
          self.title = featureable.send(attr)
          break
        end
      end
    end
  end

  def assign_summary
    # If there is no summary given, check the model.
    # Will check for the attributes in ActsAsFeatureable.auto_summary_assign_list.
    unless self.summary
      ActsAsFeatureable.auto_summary_assign_list.each do |attr|
        if featureable.respond_to?(attr)
          self.summary = featureable.send(attr)
          break
        end
      end
    end
  end

  def assign_position
    # If there is no position given, or the position given is already taken,
    # assign the lowest open position. Otherwise assign it.
    all_positions = if ActsAsFeatureable.categories
      Feature.select('features.position, features.category').where(category: self.category).map(&:position).sort
    else
      Feature.select('features.position').map(&:position).sort
    end

    if !self.position || (self.position && all_positions.include?(self.position))
      # Find lowest, non-taken position
      (1..ActsAsFeatureable.feature_limit).each do |n|
        next if all_positions.include? n
        self.position = n
        break
      end
    end
  end

  def feature_model.method_missing(mehtod_name, *args, &block)
    if ActsAsFeatureable.categories &&
        ActsAsFeatureable.categories.include?(mehtod_name)
      self.scope mehtod_name, -> { self.where(category: mehtod_name) }
      self.send(mehtod_name)
    else
      super
    end
  end

  def respond_to_missing?(method_name, include_private = false)
    (ActsAsFeatureable.categories &&
        ActsAsFeatureable.categories.include?(method_name)) ||
      super
  end
end

Instance Method Details

#assign_positionObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/acts_as_featureable/feature_additions.rb', line 59

def assign_position
  # If there is no position given, or the position given is already taken,
  # assign the lowest open position. Otherwise assign it.
  all_positions = if ActsAsFeatureable.categories
    Feature.select('features.position, features.category').where(category: self.category).map(&:position).sort
  else
    Feature.select('features.position').map(&:position).sort
  end

  if !self.position || (self.position && all_positions.include?(self.position))
    # Find lowest, non-taken position
    (1..ActsAsFeatureable.feature_limit).each do |n|
      next if all_positions.include? n
      self.position = n
      break
    end
  end
end

#assign_summaryObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/acts_as_featureable/feature_additions.rb', line 46

def assign_summary
  # If there is no summary given, check the model.
  # Will check for the attributes in ActsAsFeatureable.auto_summary_assign_list.
  unless self.summary
    ActsAsFeatureable.auto_summary_assign_list.each do |attr|
      if featureable.respond_to?(attr)
        self.summary = featureable.send(attr)
        break
      end
    end
  end
end

#assign_titleObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/acts_as_featureable/feature_additions.rb', line 33

def assign_title
  # If there is no title given, check the model.
  # Will check for the attributes in ActsAsFeatureable.auto_title_assign_list.
  unless self.title
    ActsAsFeatureable.auto_title_assign_list.each do |attr|
      if featureable.respond_to?(attr)
        self.title = featureable.send(attr)
        break
      end
    end
  end
end

#ensure_categoriesObject



18
19
20
21
22
23
24
# File 'lib/acts_as_featureable/feature_additions.rb', line 18

def ensure_categories
  cats = ::ActsAsFeatureable::categories
  # Allow all categories if set to false
  if cats && self.category && !cats.include?(self.category.to_sym)
    errors.add(:category, " is not in the list [#{cats.join(',')}]")
  end
end

#feature_limit_not_reachedObject



26
27
28
29
30
31
# File 'lib/acts_as_featureable/feature_additions.rb', line 26

def feature_limit_not_reached
  limit = ActsAsFeatureable.feature_limit
  errors.add(:base,
    "The feature limit of #{limit} has been reached. Please delete or change an existing feature.") if
    ActsAsFeatureable.categories ? Feature.where(category: self.category).count >= limit : Feature.count >= limit
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/acts_as_featureable/feature_additions.rb', line 88

def respond_to_missing?(method_name, include_private = false)
  (ActsAsFeatureable.categories &&
      ActsAsFeatureable.categories.include?(method_name)) ||
    super
end