Skip to content

Commit

Permalink
Merge pull request #48 from hyoshida/sort-records-in-admin
Browse files Browse the repository at this point in the history
Sort records in admin
  • Loading branch information
YOSHIDA Cake committed Oct 26, 2015
2 parents 887ec29 + 2545371 commit c7e14c2
Show file tree
Hide file tree
Showing 31 changed files with 75 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class NavigationsController < Comable::Admin::ApplicationController

def index
@q = Comable::Navigation.ransack(params[:q])
@navigations = @q.result.accessible_by(current_ability)
@navigations = @q.result.accessible_by(current_ability).by_newest
end

def show
Expand Down
2 changes: 1 addition & 1 deletion backend/app/controllers/comable/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class PagesController < Comable::Admin::ApplicationController

def index
@q = Comable::Page.ransack(params[:q])
@pages = @q.result.accessible_by(current_ability)
@pages = @q.result.accessible_by(current_ability).by_newest
end

def show
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PaymentMethodsController < Comable::Admin::ApplicationController
load_and_authorize_resource class: Comable::PaymentMethod.name

def index
@payment_methods = @payment_methods.page(params[:page])
@payment_methods = @payment_methods.page(params[:page]).by_newest
end

def show
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ProductsController < Comable::Admin::ApplicationController

def index
@q = Comable::Product.ransack(params[:q])
@products = @q.result(distinct: true).includes(:images, variants: [:option_values, :stock]).page(params[:page]).accessible_by(current_ability)
@products = @q.result(distinct: true).includes(:images, variants: [:option_values, :stock]).page(params[:page]).accessible_by(current_ability).by_newest
end

def show
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ShipmentMethodsController < Comable::Admin::ApplicationController
load_and_authorize_resource class: Comable::ShipmentMethod.name

def index
@shipment_methods = @shipment_methods.page(params[:page])
@shipment_methods = @shipment_methods.page(params[:page]).by_newest
end

def show
Expand Down
2 changes: 1 addition & 1 deletion backend/app/controllers/comable/admin/stocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class StocksController < Comable::Admin::ApplicationController

def index
@q = @stocks.ransack(params[:q])
@stocks = @q.result.includes(variant: [:product, :option_values]).page(params[:page]).accessible_by(current_ability)
@stocks = @q.result.includes(variant: [:product, :option_values]).page(params[:page]).accessible_by(current_ability).by_newest
end

def show
Expand Down
1 change: 1 addition & 0 deletions backend/app/controllers/comable/admin/themes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ThemesController < Comable::Admin::ApplicationController
load_and_authorize_resource class: Comable::Theme.name, find_by: :name

def index
@themes = @themes.by_newest
end

def show
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TrackersController < Comable::Admin::ApplicationController
load_and_authorize_resource class: Comable::Tracker.name

def index
@trackers = @trackers.page(params[:page])
@trackers = @trackers.page(params[:page]).by_newest
end

def show
Expand Down
2 changes: 1 addition & 1 deletion backend/app/controllers/comable/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UsersController < Comable::Admin::ApplicationController

def index
@q = Comable::User.ransack(params[:q])
@users = @q.result.page(params[:page]).accessible_by(current_ability)
@users = @q.result.page(params[:page]).accessible_by(current_ability).by_newest
end

def show
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class VariantsController < Comable::Admin::ApplicationController

def index
@q = @variants.ransack(params[:q])
@variants = @q.result.includes(:product).page(params[:page]).accessible_by(current_ability)
@variants = @q.result.includes(:product).page(params[:page]).accessible_by(current_ability).by_newest
end

def show
Expand Down
2 changes: 2 additions & 0 deletions core/app/models/comable/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ class Navigation < ActiveRecord::Base

validates :name, length: { maximum: 255 }, presence: true
validates :navigation_items, presence: true

scope :by_newest, -> { reorder(created_at: :desc) }
end
end
4 changes: 3 additions & 1 deletion core/app/models/comable/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Page < ActiveRecord::Base
include Comable::Ransackable
extend FriendlyId

PREVIEW_SESSION_KEY = :preview_page

friendly_id :title, use: :slugged

validates :title, length: { maximum: 255 }, presence: true
Expand All @@ -12,7 +14,7 @@ class Page < ActiveRecord::Base
validates :meta_keywords, length: { maximum: 255 }
validates :slug, length: { maximum: 255 }, presence: true, uniqueness: true

PREVIEW_SESSION_KEY = :preview_page
scope :by_newest, -> { reorder(created_at: :desc) }

def published?
published_at.present? && published_at <= Time.now
Expand Down
2 changes: 2 additions & 0 deletions core/app/models/comable/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class PaymentMethod < ActiveRecord::Base
validates :enable_price_from, numericality: { greater_than_or_equal_to: 0, allow_blank: true }
validates :enable_price_to, numericality: { greater_than_or_equal_to: 0, allow_blank: true }

scope :by_newest, -> { reorder(created_at: :desc) }

def payment_provider
return unless Object.const_defined?(payment_provider_type)
Object.const_get(payment_provider_type)
Expand Down
1 change: 1 addition & 0 deletions core/app/models/comable/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Product < ActiveRecord::Base
accepts_nested_attributes_for :images, allow_destroy: true

scope :published, -> (published_at = nil) { where('published_at <= ?', published_at || Time.now) }
scope :by_newest, -> { reorder(created_at: :desc) }

validates :name, presence: true, length: { maximum: 255 }

Expand Down
1 change: 1 addition & 0 deletions core/app/models/comable/shipment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ class ShipmentMethod < ActiveRecord::Base
validates :traking_url, length: { maximum: 255 }

scope :activated, -> { where(activated_flag: true) }
scope :by_newest, -> { reorder(created_at: :desc) }
end
end
4 changes: 4 additions & 0 deletions core/app/models/comable/stock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class Stock < ActiveRecord::Base
# 品切れの在庫インスタンスを返す
scope :unstocked, -> { where('quantity <= ?', 0) }

# @!scope class
# returns sorted records by newest.
scope :by_newest, -> { reorder(created_at: :desc) }

#
# @!endgroup
#
Expand Down
2 changes: 2 additions & 0 deletions core/app/models/comable/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Theme < ActiveRecord::Base
validates :homepage, length: { maximum: 255 }
validates :author, length: { maximum: 255 }

scope :by_newest, -> { reorder(created_at: :desc) }

class << self
def dir
Rails.root.join('themes')
Expand Down
5 changes: 3 additions & 2 deletions core/app/models/comable/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ class Tracker < ActiveRecord::Base
validates :code, presence: true
validates :place, presence: true, length: { maximum: 255 }

scope :activated, -> { where(activated_flag: true) }

enumerize :place, in: %i(
everywhere
checkout
), scope: true

scope :activated, -> { where(activated_flag: true) }
scope :by_newest, -> { reorder(created_at: :desc) }
end
end
1 change: 1 addition & 0 deletions core/app/models/comable/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class User < ActiveRecord::Base
scope :this_month, -> { where(created_at: Time.now.beginning_of_month..Time.now.end_of_month) }
scope :this_week, -> { where(created_at: Time.now.beginning_of_week..Time.now.end_of_week) }
scope :last_week, -> { where(created_at: 1.week.ago.beginning_of_week..1.week.ago.end_of_week) }
scope :by_newest, -> { reorder(created_at: :desc) }

validates :email, presence: true, length: { maximum: 255 }

Expand Down
2 changes: 2 additions & 0 deletions core/app/models/comable/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Variant < ActiveRecord::Base

ransack_options attribute_select: { associations: [:product, :stock, :option_values] }, ransackable_attributes: { except: :product_id }

scope :by_newest, -> { reorder(created_at: :desc) }

def quantity
stock.try(:quantity) || build_stock.quantity
end
Expand Down
2 changes: 2 additions & 0 deletions core/spec/models/comable/navigation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
describe Comable::Navigation do
subject { create(:navigation, navigation_items: [create(:navigation_item)]) }

it { is_expected.to scope(:by_newest) { reorder(created_at: :desc) } }

describe 'associations' do
it { is_expected.to have_many(:navigation_items).class_name(Comable::NavigationItem.name) }
end
Expand Down
2 changes: 2 additions & 0 deletions core/spec/models/comable/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
it { is_expected.to validate_length_of(:meta_keywords).is_at_most(255) }
it { is_expected.to validate_length_of(:meta_description).is_at_most(255) }
end

it { is_expected.to scope(:by_newest) { reorder(created_at: :desc) } }
end
2 changes: 2 additions & 0 deletions core/spec/models/comable/payment_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
it { is_expected.to validate_numericality_of(:enable_price_from).is_greater_than_or_equal_to(0) }
it { is_expected.to validate_numericality_of(:enable_price_to).is_greater_than_or_equal_to(0) }
end

it { is_expected.to scope(:by_newest) { reorder(created_at: :desc) } }
end
2 changes: 2 additions & 0 deletions core/spec/models/comable/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_length_of(:name).is_at_most(255) }

it { is_expected.to scope(:by_newest) { reorder(created_at: :desc) } }

describe '.published' do
it 'includes published products' do
product = create(:product, published_at: Time.now)
Expand Down
2 changes: 2 additions & 0 deletions core/spec/models/comable/shipment_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@

it { is_expected.to validate_numericality_of(:fee).is_greater_than_or_equal_to(0) }
end

it { is_expected.to scope(:by_newest) { reorder(created_at: :desc) } }
end
2 changes: 2 additions & 0 deletions core/spec/models/comable/stock_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
describe Comable::Stock do
it { is_expected.to validate_numericality_of(:quantity).is_greater_than_or_equal_to(0) }

it { is_expected.to scope(:by_newest) { reorder(created_at: :desc) } }

describe '#product=' do
it 'should sets product with vatiant' do
subject.variant = build(:variant)
Expand Down
2 changes: 2 additions & 0 deletions core/spec/models/comable/theme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
it { is_expected.to validate_length_of(:homepage).is_at_most(255) }
it { is_expected.to validate_length_of(:author).is_at_most(255) }

it { is_expected.to scope(:by_newest) { reorder(created_at: :desc) } }

describe '.dir' do
it 'returns the dirctory path for themes' do
expect(described_class.dir).to eq(Rails.root.join('themes'))
Expand Down
2 changes: 2 additions & 0 deletions core/spec/models/comable/tracker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
it { is_expected.to validate_length_of(:name).is_at_most(255) }
it { is_expected.to validate_length_of(:tracker_id).is_at_most(255) }
it { is_expected.to validate_length_of(:place).is_at_most(255) }

it { is_expected.to scope(:by_newest) { reorder(created_at: :desc) } }
end
2 changes: 2 additions & 0 deletions core/spec/models/comable/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
it { is_expected.to validate_length_of(:email).is_at_most(255) }
end

it { is_expected.to scope(:by_newest) { reorder(created_at: :desc) } }

describe 'incomplete order' do
context 'when guest' do
let(:stock) { create(:stock, :stocked, :with_product) }
Expand Down
2 changes: 2 additions & 0 deletions core/spec/models/comable/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
it { is_expected.to validate_numericality_of(:price).is_greater_than_or_equal_to(0) }
it { is_expected.to validate_length_of(:sku).is_at_most(255) }

it { is_expected.to scope(:by_newest) { reorder(created_at: :desc) } }

describe '#options' do
it 'should returns name of OptionValue' do
option_value = build(:option_value)
Expand Down
24 changes: 24 additions & 0 deletions spec/support/scope_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Based on http://stackoverflow.com/questions/6853744/how-can-i-have-rspec-test-for-my-default-scope/6853925#6853925
RSpec::Matchers.define :scope do |scope|
define_method :actual_for do |subject|
subject.class.send(scope).to_sql
end

define_method :expected_for do |subject|
subject.class.instance_eval(&block_arg).to_sql
end

define_method :diff do |actual, expected|
RSpec::Support::Differ.new.diff_as_string(actual.split.join("\n"), expected.split.join("\n"))
end

match do |subject|
actual_for(subject) == expected_for(subject)
end

failure_message do |subject|
expected = expected_for(subject)
actual = actual_for(subject)
"expected the scope as `#{expected}`, but builds `#{actual}`" + diff(actual, expected)
end
end

0 comments on commit c7e14c2

Please sign in to comment.