Skip to content

Commit

Permalink
Prefer classes over hashy-syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Apr 2, 2014
1 parent 9ca0247 commit 875c9ba
Show file tree
Hide file tree
Showing 29 changed files with 997 additions and 827 deletions.
2 changes: 1 addition & 1 deletion app/controllers/rails_admin/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def check_for_cancel
end

def get_collection(model_config, scope, pagination)
associations = model_config.list.fields.select { |f| f.type == :belongs_to_association && !f.polymorphic? }.collect { |f| f.association[:name] }
associations = model_config.list.fields.select { |f| f.type == :belongs_to_association && !f.polymorphic? }.collect { |f| f.association.name }
options = {}
options = options.merge(page: (params[Kaminari.config.param_name] || 1).to_i, per: (params[:per] || model_config.list.items_per_page)) if pagination
options = options.merge(include: associations) unless associations.blank?
Expand Down
2 changes: 1 addition & 1 deletion app/views/rails_admin/main/_delete_notice.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- @abstract_model.each_associated_children(object) do |association, child|
%li
- child_config = RailsAdmin.config(child)
= @abstract_model.model.human_attribute_name association[:name]
= @abstract_model.model.human_attribute_name association.name
- wording = child.send(child_config.object_label_method)
- if child.id && (show_action = action(:show, child_config.abstract_model, child))
= link_to(wording, url_for(action: show_action.action_name, model_name: child_config.abstract_model.to_param, id: child.id), class: 'pjax')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
:ruby
type_collection = field.polymorphic_type_collection
type_column = field.association[:foreign_type].to_s
type_column = field.association.foreign_type.to_s
selected_type = field.bindings[:object].send(type_column)
collection = field.associated_collection(selected_type)
selected = field.bindings[:object].send(field.association[:name])
selected = field.bindings[:object].send(field.association.name)
column_type_dom_id = form.dom_id(field).sub(field.method_name.to_s, type_column)

= form.select type_column, type_collection, {include_blank: true, selected: selected_type}, id: column_type_dom_id, data: { polymorphic: true, urls: field.polymorphic_type_urls.to_json }
Expand Down
8 changes: 4 additions & 4 deletions app/views/rails_admin/main/export.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
.control-group
%label.control-label{rel: 'tooltip', :'data-original-title' => t('admin.export.click_to_reverse_selection'), onclick: 'jQuery(this).siblings(".controls").find("input").click()'}= t('admin.export.fields_from', name: @model_config.label_plural.downcase)
.controls
- visible_fields.select{ |f| !f.association? || f.association[:polymorphic] }.each do |field|
- visible_fields.select{ |f| !f.association? || f.association.polymorphic? }.each do |field|
- list = field.virtual? ? 'methods' : 'only'
- if field.association? && field.association[:polymorphic]
- if field.association? && field.association.polymorphic?
%label.checkbox{for: "schema_#{list}_#{field.method_name}"}
= check_box_tag "schema[#{list}][]", field.method_name, true, { id: "schema_#{list}_#{field.method_name}" }
= field.label + " [id]"
- polymorphic_type_column_name = @abstract_model.properties.find {|p| field.association[:foreign_type] == p[:name] }[:name]
- polymorphic_type_column_name = @abstract_model.properties.find {|p| field.association.foreign_type == p.name }.name
%label.checkbox{for: "schema_#{list}_#{polymorphic_type_column_name}"}
= check_box_tag "schema[#{list}][]", polymorphic_type_column_name, true, { id: "schema_#{list}_#{polymorphic_type_column_name}" }
= field.label + " [type]"
Expand All @@ -31,7 +31,7 @@
= check_box_tag "schema[#{list}][]", field.name, true, { id: "schema_#{list}_#{field.name}" }
= field.label

- visible_fields.select{ |f| f.association? && !f.association[:polymorphic] }.each do |field|
- visible_fields.select{ |f| f.association? && !f.association.polymorphic? }.each do |field|
- fields = field.associated_model_config.export.with(controller: self.controller, view: self, object: (associated_model = field.associated_model_config.abstract_model.model).new).visible_fields.select{ |f| !f.association? }
.control-group
%label.control-label{rel: 'tooltip', :'data-original-title' => t('admin.export.click_to_reverse_selection'), onclick: 'jQuery(this).siblings(".controls").find("input").click()'}= t('admin.export.fields_from_associated', name: field.label.downcase)
Expand Down
10 changes: 5 additions & 5 deletions lib/rails_admin/abstract_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def new(m)
def polymorphic_parents(adapter, model_name, name)
@@polymorphic_parents[adapter.to_sym] ||= {}.tap do |hash|
all(adapter).each do |am|
am.associations.select { |r| r[:as] }.each do |association|
(hash[[association[:model_proc].call.to_s.underscore, association[:as]].join('_').to_sym] ||= []) << am.model
am.associations.select { |r| r.as }.each do |association|
(hash[[association.klass.to_s.underscore, association.as].join('_').to_sym] ||= []) << am.model
end
end
end
Expand Down Expand Up @@ -82,13 +82,13 @@ def where(conditions)

def each_associated_children(object)
associations.each do |association|
case association[:type]
case association.type
when :has_one
if child = object.send(association[:name])
if child = object.send(association.name)
yield(association, child)
end
when :has_many
object.send(association[:name]).each do |child| # rubocop:disable ShadowingOuterLocalVariable
object.send(association.name).each do |child| # rubocop:disable ShadowingOuterLocalVariable
yield(association, child)
end
end
Expand Down
130 changes: 81 additions & 49 deletions lib/rails_admin/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def destroy(objects)

def associations
model.reflect_on_all_associations.collect do |association|
Association.new(association, model).to_options_hash
Association.new(association, model)
end
end

Expand All @@ -60,13 +60,7 @@ def properties
DISABLED_COLUMN_MATCHERS.any? { |matcher| matcher.match(c.type.to_s) }
end
columns.collect do |property|
{
name: property.name.to_sym,
pretty_name: property.name.to_s.tr('_', ' ').capitalize,
length: property.limit,
nullable?: property.null,
serial?: property.primary,
}.merge(type_lookup(property))
Property.new(property, model)
end
end

Expand Down Expand Up @@ -145,11 +139,48 @@ def build_statement(column, type, value, operator)
StatementBuilder.new(column, type, value, operator).to_statement
end

def type_lookup(property)
if model.serialized_attributes[property.name.to_s]
{type: :serialized}
else
{type: property.type}
class Property
attr_reader :property, :model

def initialize(property, model)
@property = property
@model = model
end

def name
property.name.to_sym
end

def pretty_name
property.name.to_s.tr('_', ' ').capitalize
end

def type
if model.serialized_attributes[property.name.to_s]
:serialized
else
property.type
end
end

def length
property.limit
end

def nullable?
property.null
end

def serial?
property.primary
end

def association?
false
end

def read_only?
false
end
end

Expand All @@ -161,68 +192,69 @@ def initialize(association, model)
@model = model
end

def to_options_hash
{
name: name.to_sym,
pretty_name: display_name,
type: macro,
model_proc: proc { model_lookup },
primary_key_proc: proc { primary_key_lookup },
foreign_key: foreign_key.to_sym,
foreign_type: foreign_type_lookup,
as: as_lookup,
polymorphic: polymorphic_lookup,
inverse_of: inverse_of_lookup,
read_only: read_only_lookup,
nested_form: nested_attributes_options_lookup
}
def name
association.name.to_sym
end

private
def pretty_name
name.to_s.tr('_', ' ').capitalize
end

def type
association.macro
end

def model_lookup
def klass
if options[:polymorphic]
polymorphic_parents(:active_record, model_name.to_s, name) || []
polymorphic_parents(:active_record, model.name.to_s, name) || []
else
klass
association.klass
end
end

def foreign_type_lookup
def primary_key
(options[:primary_key] || association.klass.primary_key).try(:to_sym) unless polymorphic?
end

def foreign_key
association.foreign_key.to_sym
end

def foreign_type
options[:foreign_type].try(:to_sym) || :"#{name}_type" if options[:polymorphic]
end

def nested_attributes_options_lookup
model.nested_attributes_options.try { |o| o[name.to_sym] }
def foreign_inverse_of
nil
end

def as_lookup
def as
options[:as].try :to_sym
end

def polymorphic_lookup
!!options[:polymorphic] # rubocop:disable DoubleNegation
def polymorphic?
options[:polymorphic] || false
end

def primary_key_lookup
options[:primary_key] || klass.primary_key
def inverse_of
options[:inverse_of].try :to_sym
end

def inverse_of_lookup
options[:inverse_of].try :to_sym
def read_only?
(klass.all.instance_eval(&scope).readonly_value if scope.is_a? Proc) || false
end

def read_only_lookup
klass.all.instance_eval(&scope).readonly_value if scope.is_a? Proc
def nested_options
model.nested_attributes_options.try { |o| o[name.to_sym] }
end

def display_name
name.to_s.tr('_', ' ').capitalize
def association?
true
end

delegate :klass, :macro, :name, :options, :scope, :foreign_key,
to: :association, prefix: false
delegate :name, to: :model, prefix: true
private

delegate :options, :scope, to: :association, prefix: false
delegate :polymorphic_parents, to: RailsAdmin::AbstractModel
end

Expand Down

1 comment on commit 875c9ba

@dalpo
Copy link
Contributor

@dalpo dalpo commented on 875c9ba Apr 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! 😃

Please sign in to comment.