Class: Schofield::Generators::Attributes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/generators/schofield/attributes.rb

Instance Method Summary collapse

Constructor Details

#initializeAttributes

Returns a new instance of Attributes.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/generators/schofield/attributes.rb', line 10

def initialize
  @attributes                     = []
  @presence_validateds            = []
  @presence_validated_one_to_ones = []
  @attachments                    = []
  @required_attachments           = []
  @accessibles                    = []
  @max_numbers                    = {}
  @max_lengths                    = {}
  @references_count               = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



196
197
198
199
200
# File 'lib/generators/schofield/attributes.rb', line 196

def method_missing(method, *args, &block)
  if match_data = method.to_s.match(/^(.+)\?$/)
    self.find { |a| a.name == match_data[1] }.present?
  end
end

Instance Method Details

#attached_filesObject



93
94
95
96
97
# File 'lib/generators/schofield/attributes.rb', line 93

def attached_files
  @attachments.map { |attribute|
    "\n  has_attached_file :#{attribute.attachment_name}, {" + (attribute.image? ? image_attachment(name) : non_image_attachment(name)) + "\n  }.merge(S3_ATTACHMENT_INFO)"
    }.join("\n")
end

#attachments?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/generators/schofield/attributes.rb', line 77

def attachments?
  @attachments.any?
end

#attr_accessiblesObject



105
106
107
# File 'lib/generators/schofield/attributes.rb', line 105

def attr_accessibles
  @attr_accessibles ||= @accessibles.any? ? 'attr_accessible :' + @accessibles.join(', :') : ''
end

#attr_accessibles?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/generators/schofield/attributes.rb', line 73

def attr_accessibles?
  attr_accessibles.present?
end

#attr_accessorsObject



99
100
101
102
103
# File 'lib/generators/schofield/attributes.rb', line 99

def attr_accessors
  @attr_accessors ||= (@attachments.any? ? 'attr_accessor :' + @attachments.map { |attribute|
    %w( _content_type _file_size ).map{ |suffix| "#{attribute.attachment_name}#{suffix}" } + (attribute.image? ? ["#{attribute.attachment_name}_dimensions"] : [])
  }.flatten.join(', :') : '')
end

#attr_accessors?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/generators/schofield/attributes.rb', line 69

def attr_accessors?
  attr_accessors.present?
end

#eachObject



57
58
59
# File 'lib/generators/schofield/attributes.rb', line 57

def each
  @attributes.each { |a| yield a }
end

#join?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/generators/schofield/attributes.rb', line 61

def join?
  @references_count == @attributes.select{ |a| a.name != 'position' }.count && @references_count == 2
end

#new_attribute(column, one_to_one) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/schofield/attributes.rb', line 23

def new_attribute column, one_to_one

  attribute = Attribute.new column
  @attributes << attribute

  @references_count += 1 if attribute.reference?

  if attribute.validate_presence?
    if one_to_one then @presence_validated_one_to_ones << attribute
    else @presence_validateds << attribute
    end
  end

  if attribute.attachment?
    @attachments << attribute
    @required_attachments << attribute if attribute.required_attachment?
  end

  if attribute.max_number
    @max_numbers[attribute.max_number] ||= []
    @max_numbers[attribute.max_number] << attribute.name
  end

  if attribute.max_length
    @max_lengths[attribute.max_length] ||= []
    @max_lengths[attribute.max_length] << attribute.name
  end

  accessible_name = attribute.accessible_name
  @accessibles << accessible_name if accessible_name.present?

  attribute
end

#to_s_stringObject



85
86
87
88
89
90
91
# File 'lib/generators/schofield/attributes.rb', line 85

def to_s_string
  case
  when name?  then 'name'
  when title? then 'title'
  when text?  then 'text'
  end
end

#validationsObject



81
82
83
# File 'lib/generators/schofield/attributes.rb', line 81

def validations
  @validations ||= get_validations
end

#validations?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/generators/schofield/attributes.rb', line 65

def validations?
  validations.any?
end