Class: Releaf::ResourceBase

Inherits:
Object
  • Object
show all
Defined in:
app/lib/releaf/resource_base.rb

Direct Known Subclasses

ResourceFields, ResourceParams

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_class) ⇒ ResourceBase

Returns a new instance of ResourceBase.



4
5
6
# File 'app/lib/releaf/resource_base.rb', line 4

def initialize(resource_class)
  self.resource_class = resource_class
end

Instance Attribute Details

#resource_classObject

Returns the value of attribute resource_class.



2
3
4
# File 'app/lib/releaf/resource_base.rb', line 2

def resource_class
  @resource_class
end

Class Method Details

.title(resource) ⇒ Object



57
58
59
60
61
# File 'app/lib/releaf/resource_base.rb', line 57

def self.title(resource)
  title_methods.each do|method_name|
    return resource.send(method_name) if resource.respond_to?(method_name)
  end
end

.title_methodsObject



63
64
65
# File 'app/lib/releaf/resource_base.rb', line 63

def self.title_methods
  [:releaf_title, :name, :title, :to_s]
end

Instance Method Details

#association_attributes(association) ⇒ Object



28
29
30
# File 'app/lib/releaf/resource_base.rb', line 28

def association_attributes(association)
  self.class.new(association.klass).values - association_excluded_attributes(association)
end

#association_excluded_attributes(association) ⇒ Object



32
33
34
# File 'app/lib/releaf/resource_base.rb', line 32

def association_excluded_attributes(association)
  [association.foreign_key, association.type].compact.map(&:to_s)
end

#associationsObject



36
37
38
39
40
# File 'app/lib/releaf/resource_base.rb', line 36

def associations
  resource_class.reflect_on_all_associations.collect do |association|
    association if includable_association?(association)
  end.compact
end

#associations_attributesObject



22
23
24
25
26
# File 'app/lib/releaf/resource_base.rb', line 22

def associations_attributes
  associations.collect do |association|
    {association.name => association_attributes(association)}
  end
end

#base_attributesObject



12
13
14
# File 'app/lib/releaf/resource_base.rb', line 12

def base_attributes
  resource_class.column_names
end

#excluded_associationsObject



53
54
55
# File 'app/lib/releaf/resource_base.rb', line 53

def excluded_associations
  [:translations]
end

#excluded_attributesObject



8
9
10
# File 'app/lib/releaf/resource_base.rb', line 8

def excluded_attributes
  %w{id created_at updated_at}
end

#includable_association?(association) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'app/lib/releaf/resource_base.rb', line 42

def includable_association?(association)
  includable_association_types.include?(association.macro) &&
    excluded_associations.exclude?(association.name) &&
    association.class != ActiveRecord::Reflection::ThroughReflection &&
    resource_class.nested_attributes_options.has_key?(association.name)
end

#includable_association_typesObject



49
50
51
# File 'app/lib/releaf/resource_base.rb', line 49

def includable_association_types
  [:has_many, :has_one]
end

#values(include_associations: true) ⇒ Object



16
17
18
19
20
# File 'app/lib/releaf/resource_base.rb', line 16

def values(include_associations: true)
  list = base_attributes - excluded_attributes
  list += associations_attributes if include_associations
  list
end