Module: ActsAsParanoid
- Defined in:
- lib/rails4_acts_as_paranoid.rb
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Constant Summary
collapse
- DEFAULT_CONFIG =
{
:column => "deleted_at",
:column_type => "time",
:recover_dependent_associations => true,
:dependent_recovery_window => 2.minutes
}
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.default_config=(config = {}) ⇒ Object
88
89
90
|
# File 'lib/rails4_acts_as_paranoid.rb', line 88
def self.default_config=(config={})
DEFAULT_CONFIG.merge! config
end
|
Instance Method Details
#acts_as_paranoid(options = {}) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/rails4_acts_as_paranoid.rb', line 135
def acts_as_paranoid(options = {})
raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?
class_attribute :paranoid_configuration, :paranoid_column_reference
options = DEFAULT_CONFIG.merge(options)
if options[:columns]
primary_column = options[:columns].first
secondary_columns = options[:columns][1..-1]
else
primary_column = options
secondary_columns = []
end
self.paranoid_configuration = {
:primary_column => build_column_config(primary_column),
:secondary_columns => secondary_columns.map { |column| build_column_config(column) }
}
self.paranoid_column_reference = "#{self.table_name}.#{primary_paranoid_column[:column]}"
return if paranoid?
default_scope { where(*is_not_paranoid_deleted) }
scope :paranoid_deleted_around_time, lambda {|value, window|
if self.class.respond_to?(:paranoid?) && self.class.paranoid?
if self.class.paranoid_column_type == 'time' && ![true, false].include?(value)
self.where("#{self.class.paranoid_column} > ? AND #{self.class.paranoid_column} < ?", (value - window), (value + window))
else
self.only_deleted
end
end if primary_paranoid_column[:column_type] == 'time'
}
include InstanceMethods
extend ClassMethods
end
|
#build_column_config(options = {}) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/rails4_acts_as_paranoid.rb', line 122
def build_column_config(options={})
column = DEFAULT_CONFIG.dup
column.delete(:columns)
column = column.merge(:deleted_value => "deleted") if options[:column_type] == "string"
column = column.merge(options)
unless ['time', 'boolean', 'string'].include? column[:column_type]
raise ArgumentError, "'time', 'boolean' or 'string' expected for :column_type option, got #{column[:column_type]}"
end
column
end
|
#is_not_paranoid_deleted ⇒ Object
96
97
98
99
|
# File 'lib/rails4_acts_as_paranoid.rb', line 96
def is_not_paranoid_deleted
operator = non_deleted_value.nil? ? "IS" : "="
["#{paranoid_column_reference} #{operator} ?", non_deleted_value]
end
|
#is_paranoid_deleted ⇒ Object
101
102
103
104
|
# File 'lib/rails4_acts_as_paranoid.rb', line 101
def is_paranoid_deleted
operator = non_deleted_value.nil? ? "IS NOT" : "!="
["#{paranoid_column_reference} #{operator} ?", non_deleted_value]
end
|
#non_deleted_value ⇒ Object
106
107
108
|
# File 'lib/rails4_acts_as_paranoid.rb', line 106
def non_deleted_value
primary_paranoid_column[:column_type] == "boolean" ? false : nil
end
|
#paranoid? ⇒ Boolean
92
93
94
|
# File 'lib/rails4_acts_as_paranoid.rb', line 92
def paranoid?
self.included_modules.include?(InstanceMethods)
end
|
#primary_paranoid_column ⇒ Object
114
115
116
|
# File 'lib/rails4_acts_as_paranoid.rb', line 114
def primary_paranoid_column
self.paranoid_configuration[:primary_column]
end
|
#secondary_paranoid_columns ⇒ Object
118
119
120
|
# File 'lib/rails4_acts_as_paranoid.rb', line 118
def secondary_paranoid_columns
self.paranoid_configuration[:secondary_columns]
end
|
#validates_as_paranoid ⇒ Object