Class: Releaf::Builders::Utilities::ResolveAttributeFieldMethodName

Inherits:
Object
  • Object
show all
Includes:
Service
Defined in:
app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb

Constant Summary collapse

FIELD_TYPE_RESOLVERS =
{
  string: [:image, :file, :password, :email, :link, :text],
  text: [:link, :richtext, :textarea],
  integer: [:item],
  datetime: [],
  date: [],
  time: [],
  float: [],
  decimal: [],
  boolean: [],
}

Instance Method Summary collapse

Instance Method Details

#callObject


19
20
21
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 19

def call
  "releaf_#{field_type}_#{"i18n_" if localized_attribute?}field"
end

#column_field_type_resolversObject


40
41
42
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 40

def column_field_type_resolvers
  FIELD_TYPE_RESOLVERS[column_type]
end

#column_typeObject


27
28
29
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 27

def column_type
  @column_type ||= columns_class.columns_hash[attribute_name] ? columns_class.columns_hash[attribute_name].type : :string
end

#columns_classObject


31
32
33
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 31

def columns_class
  localized_attribute? ? object.class::Translation : object.class
end

#email?Boolean

Returns:

  • (Boolean)

56
57
58
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 56

def email?
  attribute_name.match(/(_email$|^email$)/).present?
end

#field_typeObject


23
24
25
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 23

def field_type
  column_field_type_resolvers.find{|resolver_name| send("#{resolver_name}?") == true } || column_type
end

#file?Boolean

Returns:

  • (Boolean)

48
49
50
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 48

def file?
  attribute_name.match(/_uid$/).present? && object.respond_to?(attribute_name.sub(/_uid$/, ''))
end

#image?Boolean

Returns:

  • (Boolean)

44
45
46
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 44

def image?
  attribute_name.match(/(thumbnail|image|photo|picture|avatar|logo|banner|icon)_uid$/).present? && file?
end

#item?Boolean

Returns:

  • (Boolean)

76
77
78
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 76

def item?
  attribute_name.match(/_id$/).present? && object.class.reflect_on_association(attribute_name[0..-4].to_sym).present?
end

#link?Boolean

Returns:

  • (Boolean)

60
61
62
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 60

def link?
  attribute_name.match(/(_url$|_link$|^url$|^link$)/).present?
end

#localized_attribute?Boolean

Returns:

  • (Boolean)

35
36
37
38
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 35

def localized_attribute?
  @localized_attribute ||= (object.class.respond_to?(:translates?) && object.class.translates? &&
                            object.class.translated_attribute_names.include?(attribute_name.to_sym))
end

#password?Boolean

Returns:

  • (Boolean)

52
53
54
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 52

def password?
  attribute_name.match(/(password|^pin$)/).present?
end

#richtext?Boolean

Returns:

  • (Boolean)

64
65
66
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 64

def richtext?
  attribute_name.match(/(_html$|^html$)/).present?
end

#text?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 72

def text?
  column_type == :string
end

#textarea?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'app/builders/releaf/builders/utilities/resolve_attribute_field_method_name.rb', line 68

def textarea?
  column_type == :text
end