-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introducing resource attributes module to build attributes once per r…
…esource
- Loading branch information
Showing
22 changed files
with
154 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module ActiveAdmin | ||
|
||
class Resource | ||
module Attributes | ||
|
||
def default_attributes | ||
resource_class.columns.each_with_object({}) do |c, attrs| | ||
unless reject_col?(c) | ||
name = c.name.to_sym | ||
attrs[name] = (method_for_column(name) || name) | ||
end | ||
end | ||
end | ||
|
||
def method_for_column(c) | ||
resource_class.respond_to?(:reflect_on_all_associations) && foreign_methods.has_key?(c) && foreign_methods[c].name.to_sym | ||
end | ||
|
||
def foreign_methods | ||
@foreign_methods ||= resource_class.reflect_on_all_associations. | ||
select{ |r| r.macro == :belongs_to }. | ||
reject{ |r| r.chain.length > 2 && !r.options[:polymorphic] }. | ||
index_by{ |r| r.foreign_key.to_sym } | ||
end | ||
|
||
def reject_col?(c) | ||
primary_col?(c) || sti_col?(c) || counter_cache_col?(c) | ||
end | ||
|
||
def primary_col?(c) | ||
c.name == resource_class.primary_key | ||
end | ||
|
||
def sti_col?(c) | ||
c.name == resource_class.inheritance_column | ||
end | ||
|
||
def counter_cache_col?(c) | ||
c.name.end_with?('_count') | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'rails_helper' | ||
|
||
module ActiveAdmin | ||
RSpec.describe Resource, "Attributes" do | ||
let(:application) { ActiveAdmin::Application.new } | ||
let(:namespace) { ActiveAdmin::Namespace.new application, :admin } | ||
let(:resource_config) { ActiveAdmin::Resource.new namespace, Post } | ||
|
||
describe "#resource_attributes" do | ||
subject do | ||
resource_config.resource_attributes | ||
end | ||
|
||
it 'should return attributes hash' do | ||
expect(subject).to eq( author_id: :author, | ||
body: :body, | ||
created_at: :created_at, | ||
custom_category_id: :category, | ||
foo_id: :foo_id, | ||
position: :position, | ||
published_date: :published_date, | ||
starred: :starred, | ||
title: :title, | ||
updated_at: :updated_at) | ||
end | ||
end | ||
|
||
describe "#association_columns" do | ||
subject do | ||
resource_config.association_columns | ||
end | ||
|
||
it 'should return associations' do | ||
expect(subject).to eq([:author, :category]) | ||
end | ||
end | ||
|
||
describe "#content_columns" do | ||
subject do | ||
resource_config.content_columns | ||
end | ||
|
||
it 'should return columns without associations' do | ||
expect(subject).to eq([:title, :body, :published_date, :position, :starred, :foo_id, :created_at, :updated_at]) | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
Oops, something went wrong.