Class: SampleModels::Model
- Inherits:
-
Delegator
- Object
- Delegator
- SampleModels::Model
show all
- Defined in:
- lib/sample_models/model.rb
Defined Under Namespace
Classes: Association, Validation
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(ar_class) ⇒ Model
Returns a new instance of Model.
7
8
9
10
|
# File 'lib/sample_models/model.rb', line 7
def initialize(ar_class)
@ar_class = ar_class
@validations = Hash.new { |h,k| h[k] = [] }
end
|
Instance Attribute Details
#ar_class ⇒ Object
Returns the value of attribute ar_class.
5
6
7
|
# File 'lib/sample_models/model.rb', line 5
def ar_class
@ar_class
end
|
Instance Method Details
#__getobj__ ⇒ Object
12
13
14
|
# File 'lib/sample_models/model.rb', line 12
def __getobj__
@ar_class
end
|
#associations ⇒ Object
16
17
18
|
# File 'lib/sample_models/model.rb', line 16
def associations
@ar_class.reflect_on_all_associations.map { |a| Association.new(a) }
end
|
#belongs_to_association(name) ⇒ Object
20
21
22
|
# File 'lib/sample_models/model.rb', line 20
def belongs_to_association(name)
belongs_to_associations.detect { |a| a.name.to_s == name.to_s }
end
|
#belongs_to_associations ⇒ Object
24
25
26
|
# File 'lib/sample_models/model.rb', line 24
def belongs_to_associations
associations.select { |a| a.belongs_to? }
end
|
#has_many_associations ⇒ Object
28
29
30
|
# File 'lib/sample_models/model.rb', line 28
def has_many_associations
associations.select { |a| a.has_many? }
end
|
#record_validation(*args) ⇒ Object
32
33
34
35
36
37
38
39
|
# File 'lib/sample_models/model.rb', line 32
def record_validation(*args)
type = args.shift
config = args.
fields = args
fields.each do |field|
@validations[field.to_s] << Validation.new(type, config)
end
end
|
#unique?(field, value) ⇒ Boolean
41
42
43
|
# File 'lib/sample_models/model.rb', line 41
def unique?(field, value)
@ar_class.count(:conditions => {field => value}) == 0
end
|
#validated_attr_accessors ⇒ Object
45
46
47
48
49
|
# File 'lib/sample_models/model.rb', line 45
def validated_attr_accessors
@validations.keys.select { |key|
columns.none? { |column| column.name.to_s == key.to_s }
}
end
|
#validations(name) ⇒ Object
51
52
53
|
# File 'lib/sample_models/model.rb', line 51
def validations(name)
@validations[name.to_s]
end
|