app/services/resources_service.rb in simpleadmin-1.2.4 vs app/services/resources_service.rb in simpleadmin-1.2.5

- old
+ new

@@ -4,18 +4,23 @@ def initialize(model_klass_name, model_fields) @model_klass = model_klass_name.constantize @model_fields = model_fields.map { |field_attributes| field_attributes[:field_name] } end - def index_action(per_page, page, sort, query, model_attributes) + def index_action(per_page, page, sort, query, model_attributes, reflection_tables, reflection_columns) per_page = per_page.to_i page = page.to_i if page.present? total = model_klass.count resources = model_klass.limit(per_page) + if reflection_tables.present? + resources = resources.includes(reflection_tables) + model_fields.push(*reflection_columns) + end + if query.present? search_result = search(query, model_klass, model_attributes) resources = search_result[:resources] total = search_result[:total] @@ -27,23 +32,11 @@ resources.order(sort[:column_name] => sort[:order]) else resources.order(id: :asc) end - resources = resources.map do |resource| - _resource = {} - - resource.attributes.each do |attribute_name, attribute_value| - next unless model_fields.include?(attribute_name) - - _resource[attribute_name] = attribute_value - end - - _resource - end - OpenStruct.new( - resources: resources, + resources: resources.pluck(*model_fields).map { |resource| model_fields.zip(resource).to_h }, total: total ) end private