Class: Schofield::Generators::Level

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/schofield/level.rb

Constant Summary collapse

COLUMNS_TO_IGNORE =
%w( ^created_at$ ^updated_at$ _content_type$ _file_size$ _updated_at$ _type$ ^type$ ^crypted_password$ ^password_salt$ ^persistence_token$ ^perishable_token$ ^login_count$ ^failed_login_count$ ^last_request_at$ ^current_login_at$ ^last_login_at$ ^current_login_ip$ ^last_login_ip$ )

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, superclass = nil) ⇒ Level

Returns a new instance of Level.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/schofield/level.rb', line 33

def initialize model, superclass=nil
  @model               = model
  @name                = model.name.underscore
  @human_name          = @name.gsub('_', ' ')
  @superclass          = superclass
  @subclasses          = []
  @parent_associations = []
  @child_associations  = []
  add_parent_associations
  add_attributes
end

Class Attribute Details

.columns_to_ignoreObject (readonly)

Returns the value of attribute columns_to_ignore.



9
10
11
# File 'lib/generators/schofield/level.rb', line 9

def columns_to_ignore
  @columns_to_ignore
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



16
17
18
# File 'lib/generators/schofield/level.rb', line 16

def attributes
  @attributes
end

#child_associationsObject

Returns the value of attribute child_associations.



17
18
19
# File 'lib/generators/schofield/level.rb', line 17

def child_associations
  @child_associations
end

#modelObject (readonly)

Returns the value of attribute model.



16
17
18
# File 'lib/generators/schofield/level.rb', line 16

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/generators/schofield/level.rb', line 16

def name
  @name
end

#parent_associationsObject (readonly)

Returns the value of attribute parent_associations.



16
17
18
# File 'lib/generators/schofield/level.rb', line 16

def parent_associations
  @parent_associations
end

#subclassesObject

Returns the value of attribute subclasses.



17
18
19
# File 'lib/generators/schofield/level.rb', line 17

def subclasses
  @subclasses
end

#superclassObject (readonly)

Returns the value of attribute superclass.



16
17
18
# File 'lib/generators/schofield/level.rb', line 16

def superclass
  @superclass
end

Class Method Details

.tables_to_ignore=(tables) ⇒ Object



11
12
13
# File 'lib/generators/schofield/level.rb', line 11

def self.tables_to_ignore= tables
  @columns_to_ignore = ( COLUMNS_TO_IGNORE + tables.map{ |m| "^#{m.singularize}_id$" } )
end

Instance Method Details

#ancestryObject

Association ancestry



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/generators/schofield/level.rb', line 173

def ancestry
  level = self
  nested_ins = []
  next_name = nil
  while level
    if next_name
      this_name = next_name
      next_name = nil
    else
      this_name = level.name
    end
    nested_ins << this_name
    next_name = level.polymorphic_name if level.polymorphic?
    level = level.parent_associations.select(&:nest?).map(&:parent).first
    # polymorphic model could have more than one parent!!!
  end
  nested_ins.reverse
end

#attribute_of_nesting_parent?(attribute) ⇒ Boolean

Form fields

Returns:

  • (Boolean)


195
196
197
# File 'lib/generators/schofield/level.rb', line 195

def attribute_of_nesting_parent? attribute
  attribute.model_name && parent_associations.select(&:nest?).map(&:parent_name).include?(attribute.model_name)
end

#belongs_to?Boolean

Associations and cardinality

Returns:

  • (Boolean)


104
105
106
# File 'lib/generators/schofield/level.rb', line 104

def belongs_to?
  @parent_associations.any?
end

#belongs_to_one?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/generators/schofield/level.rb', line 112

def belongs_to_one?
  @parent_associations.any?(&:one_to_one?)
end

#belongs_to_one_namesObject



108
109
110
# File 'lib/generators/schofield/level.rb', line 108

def belongs_to_one_names
  @parent_associations.select(&:one_to_one?).map(&:parent_name)
end

#controllers?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/generators/schofield/level.rb', line 139

def controllers?
  !superclass? && name != 'user' && !belongs_to_one?
end

#form_field?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/generators/schofield/level.rb', line 203

def form_field? attribute
  !%w( position slug ).include?(attribute.name) && attribute.name !~ /_fingerprint$/ && !polymorphic_attribute?(attribute) && !attribute_of_nesting_parent?(attribute)
end

#has_maniesObject



128
129
130
# File 'lib/generators/schofield/level.rb', line 128

def has_manies
  @child_associations.select(&:one_to_many?).map(&:child)
end

#has_manies?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/generators/schofield/level.rb', line 120

def has_manies?
  @child_associations.any?(&:one_to_many?)
end

#has_onesObject



124
125
126
# File 'lib/generators/schofield/level.rb', line 124

def has_ones
  @child_associations.select(&:one_to_one?).map(&:child)
end

#has_ones?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/generators/schofield/level.rb', line 116

def has_ones?
  @child_associations.any?(&:one_to_one?)
end

#join?Boolean

Join table

Returns:

  • (Boolean)


59
60
61
# File 'lib/generators/schofield/level.rb', line 59

def join?
  @join ||= @model.columns.select { |c| c.name.match(/_id$/) }.length == 2 && @model.columns.select { |c| !%w( id created_at updated_at position ).include?(c.name) }.length == 2
end

#models?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/generators/schofield/level.rb', line 147

def models?
  name != 'user'
end

#multipart?Boolean

Form

Returns:

  • (Boolean)


166
167
168
# File 'lib/generators/schofield/level.rb', line 166

def multipart?
  attachments? || has_ones.any?(&:attachments?)
end

#nested?Boolean

Nesting

Returns:

  • (Boolean)


82
83
84
# File 'lib/generators/schofield/level.rb', line 82

def nested?
  @parent_associations.any?(&:nest?)
end

#nested_associationsObject



90
91
92
93
94
95
# File 'lib/generators/schofield/level.rb', line 90

def nested_associations
  if superclass? then []
  else
    associations = @child_associations + ( subclass? ? @superclass.child_associations : [] )
  end
end

#nested_levelsObject



97
98
99
# File 'lib/generators/schofield/level.rb', line 97

def nested_levels
  nested_associations.select(&:nest?).map(&:child)
end

#nests?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/generators/schofield/level.rb', line 86

def nests?
  @child_associations.any?(&:nest?)
end

#other_parent_name(parent_name) ⇒ Object



63
64
65
# File 'lib/generators/schofield/level.rb', line 63

def other_parent_name parent_name
  @parent_associations.find{ |a| a.parent_name != parent_name }.parent_name
end

#polymorphic?Boolean

Polymorphism

Returns:

  • (Boolean)


70
71
72
# File 'lib/generators/schofield/level.rb', line 70

def polymorphic?
  @polymorphic ||= polymorphic_name.present?
end

#polymorphic_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/generators/schofield/level.rb', line 199

def polymorphic_attribute? attribute
  polymorphic_name && attribute.model_name == polymorphic_name
end

#polymorphic_nameObject



74
75
76
77
# File 'lib/generators/schofield/level.rb', line 74

def polymorphic_name
  match_data = nil
  @polymorphic_name ||= @model.columns.find { |c| match_data = c.name.match(/^(.+)_type$/) } ? match_data[1] : nil
end

#routes?Boolean

Combos

Returns:

  • (Boolean)


135
136
137
# File 'lib/generators/schofield/level.rb', line 135

def routes?
  !nested? && !superclass? && !belongs_to_one? && !join?
end

#sortable?Boolean

Sortable

Returns:

  • (Boolean)


159
160
161
# File 'lib/generators/schofield/level.rb', line 159

def sortable?
  @sortable
end

#subclass?Boolean

Inheritence

Returns:

  • (Boolean)


48
49
50
# File 'lib/generators/schofield/level.rb', line 48

def subclass?
  @superclass.present?
end

#superclass?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/generators/schofield/level.rb', line 52

def superclass?
  @subclasses.any?
end

#tables?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/generators/schofield/level.rb', line 151

def tables?
  !belongs_to_one? && !superclass?
end

#views?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/generators/schofield/level.rb', line 143

def views?
  controllers? && !join?
end