Module: Card::Set::All::Rules

Extended by:
Card::Set
Defined in:
tmpsets/set/mod003-core/all/rules.rb

Overview

frozen_string_literal: true

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

RULE_SQL =
%(
  SELECT
    rules.id      AS rule_id,
    settings.id   AS setting_id,
    sets.id       AS set_id,
    sets.left_id  AS anchor_id,
    sets.right_id AS set_tag_id
  FROM cards rules
  JOIN cards sets     ON rules.left_id  = sets.id
  JOIN cards settings ON rules.right_id = settings.id
  WHERE     sets.type_id = #{Card::SetID}
    AND settings.type_id = #{Card::SettingID}
    AND (settings.codename != 'follow' OR rules.db_content != '')
    AND    rules.trash is false
    AND     sets.trash is false
    AND settings.trash is false;
).freeze
READ_RULE_SQL =

FIXME: "follow" hardcoded above

%(
  SELECT
    refs.referee_id AS party_id,
    read_rules.id   AS read_rule_id
  FROM cards read_rules
  JOIN card_references refs ON refs.referer_id    = read_rules.id
  JOIN cards sets           ON read_rules.left_id = sets.id
  WHERE read_rules.right_id = #{Card::ReadID}
    AND       sets.type_id  = #{Card::SetID}
    AND read_rules.trash is false
    AND       sets.trash is false;
).freeze
PREFERENCE_SQL =
%(
  SELECT
    preferences.id AS rule_id,
    settings.id    AS setting_id,
    sets.id        AS set_id,
    sets.left_id   AS anchor_id,
    sets.right_id  AS set_tag_id,
    users.id       AS user_id
  FROM cards preferences
  JOIN cards user_sets ON preferences.left_id  = user_sets.id
  JOIN cards settings  ON preferences.right_id = settings.id
  JOIN cards users     ON user_sets.right_id   = users.id
  JOIN cards sets      ON user_sets.left_id    = sets.id
  WHERE sets.type_id     = #{Card::SetID}
    AND settings.type_id = #{Card::SettingID}
    AND (%s or users.codename = 'all')
    AND sets.trash        is false
    AND settings.trash    is false
    AND users.trash       is false
    AND user_sets.trash   is false
    AND preferences.trash is false;
).freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#abstract_set?, #all_set?, #num_set_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #ensure_set, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.source_locationObject



8
# File 'tmpsets/set/mod003-core/all/rules.rb', line 8

def self.source_location; "/Users/ethan/dev/decko/gem/card/mod/core/set/all/rules.rb"; end

Instance Method Details

#is_preference?Boolean

TODO: abstract so account doesn't have to have User type.

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
# File 'tmpsets/set/mod003-core/all/rules.rb', line 78

def is_preference?
  name.parts.length > 2 &&
    (r = right(skip_modules: true)) &&
    r.type_id == Card::SettingID &&
    (set = self[0..-3, skip_modules: true]) &&
    set.type_id == Card::SetID &&
    (user = self[-2, skip_modules: true]) &&
    (user.type_id == Card::UserID || user.codename == :all)
end

#is_rule?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'tmpsets/set/mod003-core/all/rules.rb', line 66

def is_rule?
  is_standard_rule? || is_preference?
end

#is_standard_rule?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
# File 'tmpsets/set/mod003-core/all/rules.rb', line 70

def is_standard_rule?
  (r = right(skip_modules: true)) &&
    r.type_id == Card::SettingID &&
    (l = left(skip_modules: true)) &&
    l.type_id == Card::SetID
end

#preference_card_id_lookups(setting_code, options = {}) ⇒ Object



113
114
115
116
117
# File 'tmpsets/set/mod003-core/all/rules.rb', line 113

def preference_card_id_lookups setting_code, options={}
  user_id = options[:user_id] || options[:user]&.id || Auth.current_id
  return unless user_id
  ["#{setting_code}+#{AllID}", "#{setting_code}+#{user_id}"]
end


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'tmpsets/set/mod003-core/all/rules.rb', line 119

def related_sets with_self=false
  # refers to sets that users may configure from the current card -
  # NOT to sets to which the current card belongs

  # FIXME: change to use codenames!!

  sets = []
  if known? && type_id == Card::CardtypeID # FIXME: belongs in type/cardtype
    sets << ["#{name}+*type", Card::Set::Type.label(name)]
  end
  sets << ["#{name}+*self", Card::Set::Self.label(name)] if with_self
  if known? && name.simple?
    sets << ["#{name}+*right", Card::Set::Right.label(name)]
  end
  sets
end

#rule(setting_code, options = {}) ⇒ Object



88
89
90
91
92
# File 'tmpsets/set/mod003-core/all/rules.rb', line 88

def rule setting_code, options={}
  options[:skip_modules] = true
  card = rule_card setting_code, options
  card && card.db_content
end

#rule_card(setting_code, options = {}) ⇒ Object



94
95
96
# File 'tmpsets/set/mod003-core/all/rules.rb', line 94

def rule_card setting_code, options={}
  Card.fetch rule_card_id(setting_code, options), options
end

#rule_card_id(setting_code, options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'tmpsets/set/mod003-core/all/rules.rb', line 98

def rule_card_id setting_code, options={}
  fallback = options.delete :fallback

  if Card::Setting.user_specific? setting_code
    fallback, setting_code = preference_card_id_lookups setting_code, options
  end

  rule_set_keys.each do |rule_set_key|
    rule_id = self.class.rule_cache["#{rule_set_key}+#{setting_code}"]
    rule_id ||= fallback && self.class.rule_cache["#{rule_set_key}+#{fallback}"]
    return rule_id if rule_id
  end
  nil
end