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

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

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

RULE_SQL =

frozen_string_literal: true

%(
  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

Constants included from Format

Format::TEMPLATE_DIR

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

#all_set_format_mod!, #before, #define_on_format, #format, layout_method_name, #register_set_format, #view, #view_for_override, view_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Instance Method Details

#is_preference?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
# File 'tmpsets/set/mod002-core/all/rules.rb', line 73

def is_preference?
  cardname.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)


62
63
64
# File 'tmpsets/set/mod002-core/all/rules.rb', line 62

def is_rule?
  is_standard_rule? || is_preference?
end

#is_standard_rule?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'tmpsets/set/mod002-core/all/rules.rb', line 66

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



108
109
110
111
112
113
114
# File 'tmpsets/set/mod002-core/all/rules.rb', line 108

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


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'tmpsets/set/mod002-core/all/rules.rb', line 116

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? && cardname.simple?
    sets << ["#{name}+*right", Card::Set::Right.label(name)]
  end
  sets
end

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



83
84
85
86
87
# File 'tmpsets/set/mod002-core/all/rules.rb', line 83

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



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

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

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'tmpsets/set/mod002-core/all/rules.rb', line 93

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