Skip to content

Commit

Permalink
Update Rubocop config, fix offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rodriguez committed Sep 6, 2024
1 parent 7c8025f commit a47cfef
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 95 deletions.
26 changes: 24 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ AllCops:
Exclude:
- bin/*
- gemfiles/*
- spec/**/*
- spec/dummy/**/*
- lib/generators/**/*.rb

#########
Expand All @@ -27,12 +27,15 @@ Style/TrailingCommaInArrayLiteral:
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: comma

Style/BlockDelimiters:
AllowedPatterns: ['expect']

##########
# LAYOUT #
##########

Layout/LineLength:
Max: 125
Max: 150
Exclude:
- ajax-datatables-rails.gemspec

Expand Down Expand Up @@ -62,3 +65,22 @@ Layout/HashAlignment:
Naming/FileName:
Exclude:
- lib/ajax-datatables-rails.rb

#########
# RSPEC #
#########

RSpec/MultipleExpectations:
Max: 7

RSpec/NestedGroups:
Max: 6

RSpec/ExampleLength:
Max: 9

RSpec/MultipleMemoizedHelpers:
Max: 6

RSpec/NotToNot:
EnforcedStyle: to_not
1 change: 1 addition & 0 deletions lib/ajax-datatables-rails/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def build_conditions_for_datatable
end.reduce(:or)
end.compact.reduce(:and)
end

def build_conditions_for_selected_columns
search_columns.filter_map(&:search_query).reduce(:and)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
end
end

context 'child class implements view_columns' do
context 'when child class implements view_columns' do
it 'expects a hash based defining columns' do
datatable = ComplexDatatable.new(sample_params)
expect(datatable.view_columns).to be_a(Hash)
Expand Down Expand Up @@ -108,7 +108,7 @@

describe 'ORM API' do
context 'when ORM is not implemented' do
let(:datatable) { AjaxDatatablesRails::Base.new(sample_params) }
let(:datatable) { described_class.new(sample_params) }

describe '#fetch_records' do
it 'raises an error if it does not include an ORM module' do
Expand Down Expand Up @@ -139,29 +139,30 @@
describe 'it allows method override' do
let(:datatable) do
datatable = Class.new(ComplexDatatable) do
def filter_records(records)
raise NotImplementedError.new('FOO')
def filter_records(_records)
raise NotImplementedError, 'FOO'
end

def sort_records(records)
raise NotImplementedError.new('FOO')
def sort_records(_records)
raise NotImplementedError, 'FOO'
end

def paginate_records(records)
raise NotImplementedError.new('FOO')
def paginate_records(_records)
raise NotImplementedError, 'FOO'
end
end
datatable.new(sample_params)
end

describe '#fetch_records' do
it 'calls #get_raw_records' do
expect(datatable).to receive(:get_raw_records) { User.all }
allow(datatable).to receive(:get_raw_records) { User.all }
datatable.fetch_records
expect(datatable).to have_received(:get_raw_records)
end

it 'returns a collection of records' do
expect(datatable).to receive(:get_raw_records) { User.all }
allow(datatable).to receive(:get_raw_records) { User.all }
expect(datatable.fetch_records).to be_a(ActiveRecord::Relation)
end
end
Expand Down Expand Up @@ -204,7 +205,7 @@ def paginate_records(records)
context 'with additional_data' do
it 'returns a hash' do
create_list(:user, 5)
expect(datatable).to receive(:additional_data) { { foo: 'bar' } }
allow(datatable).to receive(:additional_data).and_return({ foo: 'bar' })
data = datatable.as_json
expect(data[:recordsTotal]).to eq 5
expect(data[:recordsFiltered]).to eq 5
Expand All @@ -228,9 +229,10 @@ def paginate_records(records)
end

describe '#column_data' do
let(:datatable) { ComplexDatatable.new(sample_params) }
before { datatable.params[:columns]['0'][:search][:value] = 'doe' }

let(:datatable) { ComplexDatatable.new(sample_params) }

it 'returns column data from params' do
expect(datatable.column_data(:username)).to eq('doe')
expect(datatable.column_data('username')).to eq('doe')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
before { datatable.params[:columns]['0'][:search][:value] = 'searchvalue' }

it 'is orderable' do
expect(column.orderable?).to eq(true)
expect(column.orderable?).to be(true)
end

it 'sorts nulls last' do
expect(column.nulls_last?).to eq(false)
expect(column.nulls_last?).to be(false)
end

it 'is searchable' do
expect(column.searchable?).to eq(true)
expect(column.searchable?).to be(true)
end

it 'is searched' do
expect(column.searched?).to eq(true)
expect(column.searched?).to be(true)
end

it 'has connected to id column' do
Expand Down Expand Up @@ -53,7 +53,7 @@

context 'with other ORM' do
it 'returns the corresponding model' do
expect(User).to receive(:respond_to?).with(:arel_table).and_return(false)
allow(User).to receive(:respond_to?).with(:arel_table).and_return(false)
expect(column.table).to eq User
end
end
Expand Down Expand Up @@ -87,7 +87,7 @@
end

it 'does not regex' do
expect(column.search.regexp?).to eq false
expect(column.search.regexp?).to be false
end
end

Expand All @@ -97,12 +97,6 @@
end
end

describe '#source' do
it 'is :like by default' do
expect(column.source).to eq('User.username')
end
end

describe '#search_query' do
it 'bulds search query' do
expect(column.search_query.to_sql).to include('%searchvalue%')
Expand All @@ -129,10 +123,10 @@
end

describe 'unsearchable column' do
let(:column) { datatable.datatable.columns.find{ |c| c.data == 'email_hash' } }
let(:column) { datatable.datatable.columns.find { |c| c.data == 'email_hash' } }

it 'is not searchable' do
expect(column.searchable?).to eql(false)
expect(column.searchable?).to be(false)
end
end

Expand All @@ -150,91 +144,96 @@
let(:column) { datatable.datatable.columns.find { |c| c.data == 'username' } }

it 'is a proc' do
config = column.instance_variable_get('@view_column')
config = column.instance_variable_get(:@view_column)
filter = config[:cond]
expect(filter).to be_a(Proc)
expect(filter).to receive(:call).with(column, column.formatted_value)
allow(filter).to receive(:call).with(column, column.formatted_value)
column.filter
expect(filter).to have_received(:call).with(column, column.formatted_value)
end
end

describe '#type_cast' do
let(:column) { datatable.datatable.columns.first }

it 'returns VARCHAR if :db_adapter is :pg' do
expect(datatable).to receive(:db_adapter) { :pg }
allow(datatable).to receive(:db_adapter).and_return(:pg)
expect(column.send(:type_cast)).to eq('VARCHAR')
end

it 'returns VARCHAR if :db_adapter is :postgre' do
expect(datatable).to receive(:db_adapter) { :postgre }
allow(datatable).to receive(:db_adapter).and_return(:postgre)
expect(column.send(:type_cast)).to eq('VARCHAR')
end

it 'returns VARCHAR if :db_adapter is :postgresql' do
expect(datatable).to receive(:db_adapter) { :postgresql }
allow(datatable).to receive(:db_adapter).and_return(:postgresql)
expect(column.send(:type_cast)).to eq('VARCHAR')
end

it 'returns VARCHAR if :db_adapter is :postgis' do
expect(datatable).to receive(:db_adapter) { :postgis }
allow(datatable).to receive(:db_adapter).and_return(:postgis)
expect(column.send(:type_cast)).to eq('VARCHAR')
end

it 'returns VARCHAR2(4000) if :db_adapter is :oracle' do
expect(datatable).to receive(:db_adapter) { :oracle }
allow(datatable).to receive(:db_adapter).and_return(:oracle)
expect(column.send(:type_cast)).to eq('VARCHAR2(4000)')
end

it 'returns VARCHAR2(4000) if :db_adapter is :oracleenhanced' do
expect(datatable).to receive(:db_adapter) { :oracleenhanced }
allow(datatable).to receive(:db_adapter).and_return(:oracleenhanced)
expect(column.send(:type_cast)).to eq('VARCHAR2(4000)')
end

it 'returns CHAR if :db_adapter is :mysql2' do
expect(datatable).to receive(:db_adapter) { :mysql2 }
allow(datatable).to receive(:db_adapter).and_return(:mysql2)
expect(column.send(:type_cast)).to eq('CHAR')
end

it 'returns CHAR if :db_adapter is :trilogy' do
expect(datatable).to receive(:db_adapter) { :trilogy }
allow(datatable).to receive(:db_adapter).and_return(:trilogy)
expect(column.send(:type_cast)).to eq('CHAR')
end

it 'returns CHAR if :db_adapter is :mysql' do
expect(datatable).to receive(:db_adapter) { :mysql }
allow(datatable).to receive(:db_adapter).and_return(:mysql)
expect(column.send(:type_cast)).to eq('CHAR')
end

it 'returns TEXT if :db_adapter is :sqlite' do
expect(datatable).to receive(:db_adapter) { :sqlite }
allow(datatable).to receive(:db_adapter).and_return(:sqlite)
expect(column.send(:type_cast)).to eq('TEXT')
end

it 'returns TEXT if :db_adapter is :sqlite3' do
expect(datatable).to receive(:db_adapter) { :sqlite3 }
allow(datatable).to receive(:db_adapter).and_return(:sqlite3)
expect(column.send(:type_cast)).to eq('TEXT')
end

it 'returns VARCHAR(4000) if :db_adapter is :sqlserver' do
expect(datatable).to receive(:db_adapter) { :sqlserver }
allow(datatable).to receive(:db_adapter).and_return(:sqlserver)
expect(column.send(:type_cast)).to eq('VARCHAR(4000)')
end
end

describe 'when empty column' do
before { datatable.params[:columns]['0'][:data] = '' }

let(:message) { 'Unknown column. Check that `data` field is filled on JS side with the column name' }

it 'raises error' do
expect { datatable.to_json }.to raise_error(AjaxDatatablesRails::Error::InvalidSearchColumn).with_message('Unknown column. Check that `data` field is filled on JS side with the column name')
expect { datatable.to_json }.to raise_error(AjaxDatatablesRails::Error::InvalidSearchColumn).with_message(message)
end
end

describe 'when unknown column' do
before { datatable.params[:columns]['0'][:data] = 'foo' }

let(:message) { "Check that column 'foo' exists in view_columns" }

it 'raises error' do
expect { datatable.to_json }.to raise_error(AjaxDatatablesRails::Error::InvalidSearchColumn).with_message("Check that column 'foo' exists in view_columns")
expect { datatable.to_json }.to raise_error(AjaxDatatablesRails::Error::InvalidSearchColumn).with_message(message)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

shared_examples 'order methods' do
it 'is orderable' do
expect(datatable.orderable?).to eq(true)
expect(datatable.orderable?).to be(true)
end

it 'is not orderable' do
datatable.options[:order] = nil
expect(datatable.orderable?).to eq(false)
expect(datatable.orderable?).to be(false)
end

it 'has 2 orderable columns' do
Expand Down Expand Up @@ -57,19 +57,20 @@
describe 'with json params' do
let(:order_option) { order_option_json }
let(:datatable) { datatable_json }

it_behaves_like 'order methods'
it_behaves_like 'columns methods'
end

describe 'search methods' do
it 'is searchable' do
datatable.options[:search][:value] = 'atom'
expect(datatable.searchable?).to eq(true)
expect(datatable.searchable?).to be(true)
end

it 'is not searchable' do
datatable.options[:search][:value] = nil
expect(datatable.searchable?).to eq(false)
expect(datatable.searchable?).to be(false)
end

it 'child class' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:parent) { ComplexDatatable.new(sample_params) }
let(:datatable) { parent.datatable }
let(:options) { ActiveSupport::HashWithIndifferentAccess.new({ 'column' => '1', 'dir' => 'desc' }) }
let(:simple_order) { AjaxDatatablesRails::Datatable::SimpleOrder.new(datatable, options) }
let(:simple_order) { described_class.new(datatable, options) }

describe 'option methods' do
it 'sql query' do
Expand All @@ -32,7 +32,7 @@
describe 'using column option' do
let(:parent) { DatatableOrderNullsLast.new(sample_params) }
let(:sorted_datatable) { parent.datatable }
let(:nulls_last_order) { AjaxDatatablesRails::Datatable::SimpleOrder.new(sorted_datatable, options) }
let(:nulls_last_order) { described_class.new(sorted_datatable, options) }

context 'with postgres database adapter' do
before { parent.db_adapter = :pg }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RSpec.describe AjaxDatatablesRails::Datatable::SimpleSearch do

let(:options) { ActiveSupport::HashWithIndifferentAccess.new({ 'value' => 'search value', 'regex' => 'true' }) }
let(:simple_search) { AjaxDatatablesRails::Datatable::SimpleSearch.new(options) }
let(:simple_search) { described_class.new(options) }

describe 'option methods' do
it 'regexp?' do
Expand Down
Loading

0 comments on commit a47cfef

Please sign in to comment.