Class: Crud::KlassInfo
- Inherits:
-
Hash
- Object
- Hash
- Crud::KlassInfo
- Defined in:
- app/models/crud/klass_info.rb
Class Method Summary collapse
Instance Method Summary collapse
- #associated_polymorphic_attribute(attribute) ⇒ Object
- #associated_polymorphic_class(attribute, row = nil) ⇒ Object
- #attribute_value_from_row(attribute, row) ⇒ Object
- #custom_fields(column, page = nil) ⇒ Object
- #custom_fields_contain_a_replacement(fields) ⇒ Object
- #global_custom_fields(page = nil) ⇒ Object
-
#initialize(class_name) ⇒ KlassInfo
constructor
A new instance of KlassInfo.
- #is_visible_attribute?(column, page) ⇒ Boolean
- #primary_key(klass) ⇒ Object
- #row_id(row) ⇒ Object
- #visible_attributes(page) ⇒ Object
Constructor Details
#initialize(class_name) ⇒ KlassInfo
Returns a new instance of KlassInfo.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 56 57 58 59 60 61 62 63 64 |
# File 'app/models/crud/klass_info.rb', line 5 def initialize(class_name) self[:name] = class_name.to_s namespace_parts = self[:name].match(/(.+)::(.+)/) self[:namespace] = namespace_parts ? "::#{namespace_parts[1]}::" : '' self[:name_as_sym] = self[:name].gsub(/::/,'_').underscore.to_sym self[:class] = self[:name].constantize self[:column_settings] = ::Crud.column_settings.select {|v| ["::#{self[:name]}", self[:name], 'all'].include?(v[:class].to_s)} self[:custom_fields] = ::Crud.custom_fields.select {|v| ["::#{self[:name]}", self[:name]].include?(v[:class].to_s)} self[:custom_fields].each {|field| field[:additional_partial_parameters] ||= {}} if self[:class].respond_to?(:reflections) self[:attributes] = [] self[:class].columns.each_index do |i| self[:attributes][i] = {} self[:attributes][i][:column_name] = self[:class].column_names[i] self[:attributes][i][:column_class_type] = self[:class].column_names[i].class self[:attributes][i][:column_data_type] = self[:class].columns[i].type validators_for_attribute = self[:class].validators_on(self[:attributes][i][:column_name].to_sym).map(&:class) self[:attributes][i][:required] = (Rails.version.to_f < 4.0) ? false : (validators_for_attribute.include?(ActiveModel::Validations::PresenceValidator) || validators_for_attribute.include?(ActiveRecord::Validations::PresenceValidator)) # Initialise belongs_to associations reflection_value = belongs_to_reflection_values.select { |k| k.name == self[:attributes][i][:column_name].sub(/_id$/,'').to_sym }[0] if reflection_value self[:attributes][i][:association] = reflection_value.macro self[:attributes][i][:association_polymorphic] = reflection_value.[:polymorphic] ? reflection_value.[:polymorphic] : false self[:attributes][i][:association_class] = association_class_for_reflection_value(reflection_value, :belongs_to, self[:attributes][i]) end end self[:has_one] = [] self[:has_many] = [] self[:has_and_belongs_to_many] = [] has_one_reflection_values.each_with_index do |reflection_value, i| self[:has_one][i] = {} self[:has_one][i][:association_name] = reflection_value.name self[:has_one][i][:association_class] = association_class_for_reflection_value(reflection_value, :has_one) end has_many_reflection_values.each_with_index do |reflection_value, i| self[:has_many][i] = {} self[:has_many][i][:association_name] = reflection_value.name self[:has_many][i][:association_class] = association_class_for_reflection_value(reflection_value, :has_many) end habtm_reflection_values.each_with_index do |reflection_value, i| self[:has_and_belongs_to_many][i] = {} self[:has_and_belongs_to_many][i][:association_name] = reflection_value.name self[:has_and_belongs_to_many][i][:association_class] = association_class_for_reflection_value(reflection_value, :has_and_belongs_to_many) end end end |
Class Method Details
.primary_key_is_id?(klass) ⇒ Boolean
70 71 72 |
# File 'app/models/crud/klass_info.rb', line 70 def self.primary_key_is_id?(klass) (klass.respond_to?(:primary_key) && klass.primary_key == 'id') end |
Instance Method Details
#associated_polymorphic_attribute(attribute) ⇒ Object
107 108 109 110 111 |
# File 'app/models/crud/klass_info.rb', line 107 def associated_polymorphic_attribute(attribute) self[:attributes].select do |attr| attr[:column_name] == attribute[:column_name].sub(/_id$/,"_type") end if attribute[:association_polymorphic] end |
#associated_polymorphic_class(attribute, row = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/crud/klass_info.rb', line 95 def associated_polymorphic_class(attribute, row = nil) associated_class = nil if (row) associated_attribute = associated_polymorphic_attribute(attribute) if associated_attribute && associated_attribute.length == 1 && row.attributes[associated_attribute[0][:column_name]] associated_class_name = row.attributes[associated_attribute[0][:column_name]] associated_class = associated_class_name.empty? ? nil : associated_class_name.constantize end end associated_class end |
#attribute_value_from_row(attribute, row) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/crud/klass_info.rb', line 78 def attribute_value_from_row(attribute, row) if attribute[:association] == :belongs_to association_id = row.attributes[attribute[:column_name]].to_i unless(attribute[:association_polymorphic]) association_value = attribute[:association_class].find(association_id) else associated_attribute = associated_polymorphic_attribute(attribute) if associated_attribute && associated_attribute.length == 1 association_value = row.attributes[associated_attribute[0][:column_name]].constantize.find(association_id) end end unless (association_id==0) end ["#{row.attributes[attribute[:column_name]]}", (association_value ? "#{association_value}" : nil)] end |
#custom_fields(column, page = nil) ⇒ Object
121 122 123 |
# File 'app/models/crud/klass_info.rb', line 121 def custom_fields(column, page = nil) self[:custom_fields].select {|v| (v[:column_name] ? v[:column_name] == column.to_s : false) && (v[:global] ? v[:global] == false : true) && ((page && v[:only]) ? v[:only].include?(page.to_sym) : true)} end |
#custom_fields_contain_a_replacement(fields) ⇒ Object
129 130 131 132 |
# File 'app/models/crud/klass_info.rb', line 129 def custom_fields_contain_a_replacement(fields) replacement_fields = fields.select {|v| (v[:column_replacement] ? v[:column_replacement] == true : false)} (replacement_fields.length > 0) end |
#global_custom_fields(page = nil) ⇒ Object
125 126 127 |
# File 'app/models/crud/klass_info.rb', line 125 def global_custom_fields(page = nil) self[:custom_fields].select {|v| (v[:global] ? v[:global] == true : false) && ((page && v[:only]) ? v[:only].include?(page.to_sym) : true)} end |
#is_visible_attribute?(column, page) ⇒ Boolean
117 118 119 |
# File 'app/models/crud/klass_info.rb', line 117 def is_visible_attribute?(column, page) ( self[:column_settings].select {|v| (v[:hidden_fields] ? v[:hidden_fields].include?(column.to_sym) : false) && (v[:only] ? v[:only].include?(page.to_sym) : true)} ).empty? end |
#primary_key(klass) ⇒ Object
66 67 68 |
# File 'app/models/crud/klass_info.rb', line 66 def primary_key(klass) klass.respond_to?(:primary_key) ? klass.primary_key : 'id' end |
#row_id(row) ⇒ Object
74 75 76 |
# File 'app/models/crud/klass_info.rb', line 74 def row_id(row) row.send( self.primary_key(row) ) end |
#visible_attributes(page) ⇒ Object
113 114 115 |
# File 'app/models/crud/klass_info.rb', line 113 def visible_attributes(page) self[:attributes].select { |v| is_visible_attribute?(v[:column_name], page) } end |