lib/generators/madmin/resource/resource_generator.rb in madmin-1.2.1 vs lib/generators/madmin/resource/resource_generator.rb in madmin-1.2.2
- old
+ new
@@ -26,74 +26,24 @@
end
end
private
- def associations
- model.reflections.reject { |name, association|
- # Hide these special associations
- name.starts_with?("rich_text") ||
- name.ends_with?("_attachment") ||
- name.ends_with?("_attachments") ||
- name.ends_with?("_blob") ||
- name.ends_with?("_blobs")
- }.keys
+ def model
+ @model ||= class_name.constantize
end
- def attributes
- model.attribute_names + virtual_attributes - redundant_attributes
+ def resource_builder
+ @resource_builder ||= ResourceBuilder.new(model)
end
- def virtual_attributes
- virtual = []
-
- # has_secure_password columns
- password_attributes = model.attribute_types.keys.select { |k| k.ends_with?("_digest") }.map { |k| k.delete_suffix("_digest") }
- virtual += password_attributes.map { |attr| [attr, "#{attr}_confirmation"] }.flatten
-
- # Add virtual attributes for ActionText and ActiveStorage
- model.reflections.each do |name, association|
- if name.starts_with?("rich_text")
- virtual << name.split("rich_text_").last
- elsif name.ends_with?("_attachment")
- virtual << name.split("_attachment").first
- elsif name.ends_with?("_attachments")
- virtual << name.split("_attachments").first
- end
- end
-
- virtual
+ def model_attributes
+ resource_builder.attributes
end
- def redundant_attributes
- redundant = []
+ delegate :associations, :virtual_attributes, :store_accessors, to: :resource_builder
- # has_secure_password columns
- redundant += model.attribute_types.keys.select { |k| k.ends_with?("_digest") }
-
- model.reflections.each do |name, association|
- if association.has_one?
- next
- elsif association.collection?
- next
- elsif association.polymorphic?
- redundant << "#{name}_id"
- redundant << "#{name}_type"
- elsif name.starts_with?("rich_text")
- redundant << name
- else # belongs to
- redundant << "#{name}_id"
- end
- end
-
- redundant
- end
-
- def model
- @model ||= class_name.constantize
- end
-
def formatted_options_for_attribute(name)
options = options_for_attribute(name)
return if options.blank?
", " + options.map { |key, value|
@@ -112,10 +62,10 @@
# Counter cache columns are typically not editable
elsif name.ends_with?("_count")
{form: false}
# Attributes without a database column
- elsif !model.column_names.include?(name)
+ elsif !model.column_names.include?(name) && !store_accessors.map(&:to_s).include?(name)
{index: false}
end
end
end
end