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


65
66
67
68
69
# File 'app/lib/releaf/resource_base.rb', line 65

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


71
72
73
# File 'app/lib/releaf/resource_base.rb', line 71

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

Instance Method Details

#association_attributes(association) ⇒ Object


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

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

#association_excluded_attributes(association) ⇒ Object


40
41
42
# File 'app/lib/releaf/resource_base.rb', line 40

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

#associationsObject


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

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

#associations_attributesObject


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

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

#base_attributesObject


20
21
22
# File 'app/lib/releaf/resource_base.rb', line 20

def base_attributes
  resource_class.column_names
end

#excluded_associationsObject


61
62
63
# File 'app/lib/releaf/resource_base.rb', line 61

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)

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

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


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

def includable_association_types
  [:has_many, :has_one]
end

#localized_attributesObject


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

def localized_attributes
  @localized_attributes ||= localized_attributes? ? resource_class.translated_attribute_names.map(&:to_s) : []
end

#localized_attributes?Boolean

Returns:

  • (Boolean)

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

def localized_attributes?
  resource_class.translates?
end

#values(include_associations: true) ⇒ Object


24
25
26
27
28
# File 'app/lib/releaf/resource_base.rb', line 24

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