Module: Offroad::ModelExtensions

Included in:
ActiveRecord::Base
Defined in:
lib/model_extensions.rb

Defined Under Namespace

Modules: CommonInstanceMethods, GlobalDataInstanceMethods, GroupDataInstanceMethods, HoboPermissionsInstanceMethods

Constant Summary collapse

OFFROAD_VALID_MODES =
[:group_base, :group_owned, :group_single, :global]
OFFROAD_GROUP_MODES =
[:group_base, :group_owned, :group_single]

Instance Method Summary collapse

Instance Method Details

#acts_as_offroadable(mode, opts = {}) ⇒ Object

Raises:



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
# File 'lib/model_extensions.rb', line 6

def acts_as_offroadable(mode, opts = {})
  raise ModelError.new("You can only call acts_as_offroadable once per model") if acts_as_offroadable?
  raise ModelError.new("You must specify a mode, one of " + OFFROAD_VALID_MODES.map(&:inspect).join("/")) unless OFFROAD_VALID_MODES.include?(mode)
  
  set_internal_cattr :offroad_mode, mode
  
  case mode
  when :group_owned then
    raise ModelError.new("For :group_owned models, need to specify :parent") unless opts[:parent]
    assoc = reflect_on_association(opts.delete(:parent))
    raise ModelError.new("No such parent associaton") unless assoc
    raise ModelError.new("Parent association must be a belongs_to association") unless assoc.belongs_to?
    raise ModelError.new("Parent association must be to a group data model") unless assoc.klass.offroad_group_data?

    set_internal_cattr :offroad_parent_assoc, assoc
    Offroad::note_group_owned_model(self)
  when :group_single then
    Offroad::note_group_single_model(self)
  when :group_base then
    Offroad::note_group_base_model(self)
  when :global then
    Offroad::note_global_data_model(self)
  end
  
  # We should have deleted all the options from the hash by this point
  raise ModelError.new("Unknown or inapplicable option(s) specified") unless opts.size == 0
  
  case mode
  when :group_base then
    named_scope :owned_by_offroad_group, lambda { |group| { :conditions => { :id => group.id } } }
    named_scope :offline_groups, {
      :joins =>
      "INNER JOIN \"#{Offroad::GroupState.table_name}\" ON \"#{Offroad::GroupState.table_name}\".app_group_id = \"#{table_name}\".\"#{primary_key}\""
    }
    named_scope :online_groups, {
      :joins =>
      "LEFT JOIN \"#{Offroad::GroupState.table_name}\" ON \"#{Offroad::GroupState.table_name}\".app_group_id = \"#{table_name}\".\"#{primary_key}\"",
      :conditions =>
      "\"#{Offroad::GroupState.table_name}\".app_group_id IS NULL"
    }
  when :group_owned then
    named_scope :owned_by_offroad_group, lambda { |group| args_for_ownership_scope(group) }
  when :group_single then
    named_scope :owned_by_offroad_group, lambda { |group| {
      :conditions => (Offroad::GroupState.count > 0 && group == Offroad::GroupState.first.app_group) ? "1=1" : "1=0"
    } }
  end

  if offroad_group_data?
    include GroupDataInstanceMethods
  else
    include GlobalDataInstanceMethods
  end
  include CommonInstanceMethods
  
  before_destroy :before_mirrored_data_destroy
  after_destroy :after_mirrored_data_destroy
  before_save :before_mirrored_data_save
  after_save :after_mirrored_data_save

  if Object.const_defined?(:Hobo) and included_modules.include?(Hobo::Model)
    include HoboPermissionsInstanceMethods
    [
      [:create, :save],
      [:update, :save],
      [:destroy, :destroy]
    ].each do |perm_name, check_name|
      define_method "#{perm_name}_permitted_with_offroad_check?".to_sym do
        pre_check_passed?("before_mirrored_data_#{check_name}".to_sym) && send("#{perm_name}_permitted_without_offroad_check?")
      end
      alias_method_chain "#{perm_name}_permitted?", "offroad_check"
    end
  end
end

#acts_as_offroadable?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/model_extensions.rb', line 86

def acts_as_offroadable?
  respond_to? :offroad_mode
end

#offroad_global_data?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/model_extensions.rb', line 102

def offroad_global_data?
  acts_as_offroadable? && offroad_mode == :global
end

#offroad_group_base?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/model_extensions.rb', line 94

def offroad_group_base?
  acts_as_offroadable? && offroad_mode == :group_base
end

#offroad_group_data?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/model_extensions.rb', line 98

def offroad_group_data?
  acts_as_offroadable? && OFFROAD_GROUP_MODES.include?(offroad_mode)
end

#offroad_model_stateObject



81
82
83
84
# File 'lib/model_extensions.rb', line 81

def offroad_model_state
  model_scope = Offroad::ModelState::for_model(self)
  return model_scope.first || model_scope.create
end

#safe_to_load_from_cargo_stream?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/model_extensions.rb', line 90

def safe_to_load_from_cargo_stream?
  acts_as_offroadable?
end