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