Class: Schofield::Generators::Level
- Inherits:
-
Object
- Object
- Schofield::Generators::Level
- 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
-
.columns_to_ignore ⇒ Object
readonly
Returns the value of attribute columns_to_ignore.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#child_associations ⇒ Object
Returns the value of attribute child_associations.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent_associations ⇒ Object
readonly
Returns the value of attribute parent_associations.
-
#subclasses ⇒ Object
Returns the value of attribute subclasses.
-
#superclass ⇒ Object
readonly
Returns the value of attribute superclass.
Class Method Summary collapse
Instance Method Summary collapse
-
#ancestry ⇒ Object
Association ancestry.
-
#attribute_of_nesting_parent?(attribute) ⇒ Boolean
Form fields.
-
#belongs_to? ⇒ Boolean
Associations and cardinality.
- #belongs_to_one? ⇒ Boolean
- #belongs_to_one_names ⇒ Object
- #controllers? ⇒ Boolean
- #form_field?(attribute) ⇒ Boolean
- #has_manies ⇒ Object
- #has_manies? ⇒ Boolean
- #has_ones ⇒ Object
- #has_ones? ⇒ Boolean
-
#initialize(model, superclass = nil) ⇒ Level
constructor
A new instance of Level.
-
#join? ⇒ Boolean
Join table.
- #models? ⇒ Boolean
-
#multipart? ⇒ Boolean
Form.
-
#nested? ⇒ Boolean
Nesting.
- #nested_associations ⇒ Object
- #nested_levels ⇒ Object
- #nests? ⇒ Boolean
- #other_parent_name(parent_name) ⇒ Object
-
#polymorphic? ⇒ Boolean
Polymorphism.
- #polymorphic_attribute?(attribute) ⇒ Boolean
- #polymorphic_name ⇒ Object
-
#routes? ⇒ Boolean
Combos.
-
#sortable? ⇒ Boolean
Sortable.
-
#subclass? ⇒ Boolean
Inheritence.
- #superclass? ⇒ Boolean
- #tables? ⇒ Boolean
- #views? ⇒ Boolean
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_ignore ⇒ Object (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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
16 17 18 |
# File 'lib/generators/schofield/level.rb', line 16 def attributes @attributes end |
#child_associations ⇒ Object
Returns the value of attribute child_associations.
17 18 19 |
# File 'lib/generators/schofield/level.rb', line 17 def child_associations @child_associations end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
16 17 18 |
# File 'lib/generators/schofield/level.rb', line 16 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/generators/schofield/level.rb', line 16 def name @name end |
#parent_associations ⇒ Object (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 |
#subclasses ⇒ Object
Returns the value of attribute subclasses.
17 18 19 |
# File 'lib/generators/schofield/level.rb', line 17 def subclasses @subclasses end |
#superclass ⇒ Object (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
#ancestry ⇒ Object
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
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
104 105 106 |
# File 'lib/generators/schofield/level.rb', line 104 def belongs_to? @parent_associations.any? end |
#belongs_to_one? ⇒ 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_names ⇒ Object
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
139 140 141 |
# File 'lib/generators/schofield/level.rb', line 139 def controllers? !superclass? && name != 'user' && !belongs_to_one? end |
#form_field?(attribute) ⇒ 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_manies ⇒ Object
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
120 121 122 |
# File 'lib/generators/schofield/level.rb', line 120 def has_manies? @child_associations.any?(&:one_to_many?) end |
#has_ones ⇒ Object
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
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
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
147 148 149 |
# File 'lib/generators/schofield/level.rb', line 147 def models? name != 'user' end |
#multipart? ⇒ Boolean
Form
166 167 168 |
# File 'lib/generators/schofield/level.rb', line 166 def multipart? || has_ones.any?(&:attachments?) end |
#nested? ⇒ Boolean
Nesting
82 83 84 |
# File 'lib/generators/schofield/level.rb', line 82 def nested? @parent_associations.any?(&:nest?) end |
#nested_associations ⇒ Object
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_levels ⇒ Object
97 98 99 |
# File 'lib/generators/schofield/level.rb', line 97 def nested_levels nested_associations.select(&:nest?).map(&:child) end |
#nests? ⇒ 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
70 71 72 |
# File 'lib/generators/schofield/level.rb', line 70 def polymorphic? @polymorphic ||= polymorphic_name.present? end |
#polymorphic_attribute?(attribute) ⇒ 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_name ⇒ Object
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
135 136 137 |
# File 'lib/generators/schofield/level.rb', line 135 def routes? !nested? && !superclass? && !belongs_to_one? && !join? end |
#sortable? ⇒ Boolean
Sortable
159 160 161 |
# File 'lib/generators/schofield/level.rb', line 159 def sortable? @sortable end |
#subclass? ⇒ Boolean
Inheritence
48 49 50 |
# File 'lib/generators/schofield/level.rb', line 48 def subclass? @superclass.present? end |
#superclass? ⇒ Boolean
52 53 54 |
# File 'lib/generators/schofield/level.rb', line 52 def superclass? @subclasses.any? end |
#tables? ⇒ Boolean
151 152 153 |
# File 'lib/generators/schofield/level.rb', line 151 def tables? !belongs_to_one? && !superclass? end |
#views? ⇒ Boolean
143 144 145 |
# File 'lib/generators/schofield/level.rb', line 143 def views? controllers? && !join? end |