Skip to content

Commit

Permalink
Merge pull request #2263 from DarthMax/fix-incorrect-dashboard-count
Browse files Browse the repository at this point in the history
Fix incorrect counts for capsuled models with the same name
  • Loading branch information
mshibuya committed Apr 23, 2015
2 parents 7dc30a3 + b5bef37 commit 335cdca
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
6 changes: 3 additions & 3 deletions app/views/rails_admin/main/dashboard.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- index_path = index_path(model_name: abstract_model.to_param)
- row_class = "#{cycle("odd", "even")}#{" link" if index_path} #{abstract_model.param_key}_links"
%tr{class: row_class, :"data-link" => index_path}
- last_used = @most_recent_changes[abstract_model.pretty_name]
- last_used = @most_recent_changes[abstract_model.model.name]
- active = last_used.try(:today?)
%td
%span.show= link_to capitalize_first_letter(abstract_model.config.label_plural), index_path, class: 'pjax'
Expand All @@ -21,11 +21,11 @@
= time_ago_in_words last_used
= t "admin.misc.ago"
%td
- count = @count[abstract_model.pretty_name]
- count = @count[abstract_model.model.name]
- percent = count > 0 ? (@max <= 1 ? count : ((Math.log(count+1) * 100.0) / Math.log(@max+1)).to_i) : -1
.progress{style: "margin-bottom:0px", class: "progress-#{get_indicator(percent)} #{active && 'active progress-striped'}" }
.progress-bar.animate-width-to{:class => "progress-bar-#{get_indicator(percent)}", :'data-animate-length' => ([1.0, percent].max.to_i * 20), :'data-animate-width-to' => "#{[2.0, percent].max.to_i}%", style: "width:2%"}
= @count[abstract_model.pretty_name]
= @count[abstract_model.model.name]
%td.links
%ul.inline.list-inline= menu_for :collection, abstract_model, nil, true
- if @auditing_adapter && authorized?(:history_index)
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_admin/config/actions/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class Dashboard < RailsAdmin::Config::Actions::Base
scope = @authorization_adapter && @authorization_adapter.query(:index, t)
current_count = t.count({}, scope)
@max = current_count > @max ? current_count : @max
@count[t.pretty_name] = current_count
@count[t.model.name] = current_count
next unless t.properties.detect { |c| c.name == :updated_at }
@most_recent_changes[t.pretty_name] = t.first(sort: "#{t.table_name}.updated_at").try(:updated_at)
@most_recent_changes[t.model.name] = t.first(sort: "#{t.table_name}.updated_at").try(:updated_at)
end
end
render @action.template_name, status: (flash[:error].present? ? :not_found : 200)
Expand Down
20 changes: 20 additions & 0 deletions spec/controllers/rails_admin/main_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@
expect(RailsAdmin.config(Player).abstract_model).not_to receive(:count)
controller.dashboard
end

it 'counts are different for same-named models in different modules' do
allow(RailsAdmin.config(User::Confirmed).abstract_model).to receive(:count).and_return(10)
allow(RailsAdmin.config(Comment::Confirmed).abstract_model).to receive(:count).and_return(0)

controller.dashboard
expect(controller.instance_variable_get('@count')['User::Confirmed']).to be 10
expect(controller.instance_variable_get('@count')['Comment::Confirmed']).to be 0
end

it 'most recent change dates are different for same-named models in different modules' do
user_update = 10.days.ago.to_date
comment_update = 20.days.ago.to_date
FactoryGirl.create(:user_confirmed, updated_at: user_update)
FactoryGirl.create(:comment_confirmed, updated_at: comment_update)

controller.dashboard
expect(controller.instance_variable_get('@most_recent_changes')['User::Confirmed']).to eq user_update
expect(controller.instance_variable_get('@most_recent_changes')['Comment::Confirmed']).to eq comment_update
end
end

describe '#check_for_cancel' do
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy_app/app/active_record/user/confirmed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class User
class Confirmed < User
end
end
4 changes: 4 additions & 0 deletions spec/dummy_app/app/mongoid/user/confirmed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class User
class Confirmed < User
end
end
6 changes: 6 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
factory :user do
sequence(:email) { |n| "username_#{n}@example.com" }
sequence(:password) { |_n| 'password' }

factory :user_confirmed, class: User::Confirmed
end

factory :field_test do
Expand All @@ -59,6 +61,10 @@
proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
EOF
end

factory :comment_confirmed, class: Comment::Confirmed do
content('something')
end
end

factory :ball do
Expand Down

0 comments on commit 335cdca

Please sign in to comment.