ransack 4.4.0 → 4.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Ransack
4
- describe Translate do
5
- describe '.attribute' do
6
- it 'translate namespaced attribute like AR does' do
7
- ar_translation = ::Namespace::Article.human_attribute_name(:title)
8
- ransack_translation = Ransack::Translate.attribute(
9
- :title,
10
- context: ::Namespace::Article.ransack.context
11
- )
12
- expect(ransack_translation).to eq ar_translation
13
- end
14
- end
15
- end
16
- end
data/spec/spec_helper.rb DELETED
@@ -1,65 +0,0 @@
1
- require 'ransack'
2
- require 'sham'
3
- require 'faker'
4
- require 'action_controller'
5
- require 'ransack/helpers'
6
- require 'pry'
7
- require 'simplecov'
8
- require 'byebug'
9
- require 'machinist/active_record'
10
-
11
- SimpleCov.start
12
- I18n.enforce_available_locales = false
13
- Time.zone = 'Eastern Time (US & Canada)'
14
- I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'support', '*.yml')]
15
-
16
- Dir[File.expand_path('../{helpers,support,blueprints}/*.rb', __FILE__)]
17
- .each { |f| require f }
18
-
19
- Faker::Config.random = Random.new(0)
20
- Sham.define do
21
- name { Faker::Name.name }
22
- title { Faker::Lorem.sentence }
23
- body { Faker::Lorem.paragraph }
24
- salary { |index| 30000 + (index * 1000) }
25
- tag_name { Faker::Lorem.words(number: 3).join(' ') }
26
- note { Faker::Lorem.words(number: 7).join(' ') }
27
- only_admin { Faker::Lorem.words(number: 3).join(' ') }
28
- only_search { Faker::Lorem.words(number: 3).join(' ') }
29
- only_sort { Faker::Lorem.words(number: 3).join(' ') }
30
- notable_id { |id| id }
31
- end
32
-
33
- RSpec.configure do |config|
34
- config.alias_it_should_behave_like_to :it_has_behavior, 'has behavior'
35
-
36
- config.before(:suite) do
37
- message = "Running Ransack specs with #{
38
- ActiveRecord::Base.connection.adapter_name
39
- }, Active Record #{::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION
40
- } and Ruby #{RUBY_VERSION}"
41
- line = '=' * message.length
42
- puts line, message, line
43
- Schema.create
44
- SubDB::Schema.create
45
- end
46
-
47
- config.before(:all) { Sham.reset(:before_all) }
48
- config.before(:each) { Sham.reset(:before_each) }
49
-
50
- config.include RansackHelper
51
- config.include PolyamorousHelper
52
- end
53
-
54
- RSpec::Matchers.define :be_like do |expected|
55
- match do |actual|
56
- actual.gsub(/^\s+|\s+$/, '').gsub(/\s+/, ' ').strip ==
57
- expected.gsub(/^\s+|\s+$/, '').gsub(/\s+/, ' ').strip
58
- end
59
- end
60
-
61
- RSpec::Matchers.define :have_attribute_method do |expected|
62
- match do |actual|
63
- actual.attribute_method?(expected)
64
- end
65
- end
data/spec/support/en.yml DELETED
@@ -1,18 +0,0 @@
1
- en:
2
- attributes:
3
- id: 'id'
4
- ransack:
5
- attributes:
6
- person:
7
- name: Full Name
8
- only_admin: admin uSer Only
9
- salary: wages
10
- awesome: ransack is really awesome
11
- article:
12
- body: maiN BoDy
13
- activerecord:
14
- attributes:
15
- namespace/article:
16
- title: AR Namespaced Title
17
- namespace_article:
18
- title: Old Ransack Namespaced Title
@@ -1,422 +0,0 @@
1
- require 'active_record'
2
- require 'activerecord-postgis-adapter'
3
-
4
- case ENV['DB'].try(:downcase)
5
- when 'mysql', 'mysql2'
6
- # To test with MySQL: `DB=mysql bundle exec rake spec`
7
- ActiveRecord::Base.establish_connection(
8
- adapter: 'mysql2',
9
- database: 'ransack',
10
- username: ENV.fetch("MYSQL_USERNAME") { "root" },
11
- password: ENV.fetch("MYSQL_PASSWORD") { "" },
12
- encoding: 'utf8'
13
- )
14
- when 'pg', 'postgres', 'postgresql'
15
- # To test with PostgreSQL: `DB=postgresql bundle exec rake spec`
16
- ActiveRecord::Base.establish_connection(
17
- adapter: 'postgresql',
18
- database: 'ransack',
19
- username: ENV.fetch("DATABASE_USERNAME") { "postgres" },
20
- password: ENV.fetch("DATABASE_PASSWORD") { "" },
21
- host: ENV.fetch("DATABASE_HOST") { "localhost" },
22
- min_messages: 'warning'
23
- )
24
- when 'postgis'
25
- # To test with PostGIS: `DB=postgis bundle exec rake spec`
26
- ActiveRecord::Base.establish_connection(
27
- adapter: 'postgis',
28
- postgis_extension: 'postgis',
29
- database: 'ransack',
30
- username: ENV.fetch("DATABASE_USERNAME") { "postgres" },
31
- password: ENV.fetch("DATABASE_PASSWORD") { "" },
32
- host: ENV.fetch("DATABASE_HOST") { "localhost" },
33
- min_messages: 'warning'
34
- )
35
- else
36
- # Otherwise, assume SQLite3: `bundle exec rake spec`
37
- ActiveRecord::Base.establish_connection(
38
- adapter: 'sqlite3',
39
- database: ':memory:'
40
- )
41
- end
42
-
43
- # This is just a test app with no sensitive data, so we explicitly allowlist all
44
- # attributes and associations for search. In general, end users should
45
- # explicitly authorize each model, but this shows a way to configure the
46
- # unrestricted default behavior of versions prior to Ransack 4.
47
- #
48
- class ApplicationRecord < ActiveRecord::Base
49
- self.abstract_class = true
50
-
51
- def self.ransackable_attributes(auth_object = nil)
52
- authorizable_ransackable_attributes
53
- end
54
-
55
- def self.ransackable_associations(auth_object = nil)
56
- authorizable_ransackable_associations
57
- end
58
- end
59
-
60
- class Person < ApplicationRecord
61
- default_scope { order(id: :desc) }
62
- belongs_to :parent, class_name: 'Person', foreign_key: :parent_id
63
- has_many :children, class_name: 'Person', foreign_key: :parent_id
64
- has_many :articles
65
- has_many :story_articles
66
-
67
- has_many :published_articles, ->{ where(published: true) },
68
- class_name: "Article"
69
- has_many :comments
70
- has_many :authored_article_comments, through: :articles,
71
- source: :comments, foreign_key: :person_id
72
- has_many :notes, as: :notable
73
-
74
- scope :restricted, lambda { where("restricted = 1") }
75
- scope :active, lambda { where("active = 1") }
76
- scope :over_age, lambda { |y| where(["age > ?", y]) }
77
- scope :of_age, lambda { |of_age|
78
- of_age ? where("age >= ?", 18) : where("age < ?", 18)
79
- }
80
-
81
- scope :sort_by_reverse_name_asc, lambda { order(Arel.sql("REVERSE(name) ASC")) }
82
- scope :sort_by_reverse_name_desc, lambda { order("REVERSE(name) DESC") }
83
-
84
- enum :temperament, { sanguine: 1, choleric: 2, melancholic: 3, phlegmatic: 4 }
85
-
86
- alias_attribute :full_name, :name
87
-
88
- ransack_alias :term, :name_or_email
89
- ransack_alias :daddy, :parent_name
90
-
91
- ransacker :reversed_name, formatter: proc { |v| v.reverse } do |parent|
92
- parent.table[:name]
93
- end
94
-
95
- ransacker :array_people_ids,
96
- formatter: proc { |v| Person.first(2).map(&:id) } do |parent|
97
- parent.table[:id]
98
- end
99
-
100
- ransacker :array_where_people_ids,
101
- formatter: proc { |v| Person.where(id: v).map(&:id) } do |parent|
102
- parent.table[:id]
103
- end
104
-
105
- ransacker :array_people_names,
106
- formatter: proc { |v| Person.first(2).map { |p| p.id.to_s } } do |parent|
107
- parent.table[:name]
108
- end
109
-
110
- ransacker :array_where_people_names,
111
- formatter: proc { |v| Person.where(id: v).map { |p| p.id.to_s } } do |parent|
112
- parent.table[:name]
113
- end
114
-
115
- ransacker :doubled_name do |parent|
116
- Arel::Nodes::InfixOperation.new(
117
- '||', parent.table[:name], parent.table[:name]
118
- )
119
- end
120
-
121
- ransacker :sql_literal_id do
122
- Arel.sql('people.id')
123
- end
124
-
125
- ransacker :name_case_insensitive, type: :string do
126
- arel_table[:name].lower
127
- end
128
-
129
- ransacker :with_arguments, args: [:parent, :ransacker_args] do |parent, args|
130
- min, max = args
131
- query = <<-SQL
132
- (SELECT MAX(articles.title)
133
- FROM articles
134
- WHERE articles.person_id = people.id
135
- AND LENGTH(articles.body) BETWEEN #{min} AND #{max}
136
- GROUP BY articles.person_id
137
- )
138
- SQL
139
- .squish
140
- Arel.sql(query)
141
- end
142
-
143
- ransacker :article_tags, formatter: proc { |id|
144
- if Tag.exists?(id)
145
- joins(articles: :tags)
146
- .where(tags: { id: id })
147
- .distinct
148
- .select(:id).arel
149
- end
150
- } do |parent|
151
- parent.table[:id]
152
- end
153
-
154
- def self.ransackable_attributes(auth_object = nil)
155
- if auth_object == :admin
156
- authorizable_ransackable_attributes - ['only_sort']
157
- else
158
- authorizable_ransackable_attributes - ['only_sort', 'only_admin']
159
- end
160
- end
161
-
162
- def self.ransortable_attributes(auth_object = nil)
163
- if auth_object == :admin
164
- column_names + _ransackers.keys - ['only_search']
165
- else
166
- column_names + _ransackers.keys - ['only_search', 'only_admin']
167
- end
168
- end
169
- end
170
-
171
- class Musician < Person
172
- end
173
-
174
- class Article < ApplicationRecord
175
- belongs_to :person
176
- has_many :comments
177
- has_and_belongs_to_many :tags
178
- has_many :notes, as: :notable
179
- has_many :recent_notes, as: :notable
180
-
181
- alias_attribute :content, :body
182
-
183
- default_scope { where("'default_scope' = 'default_scope'") }
184
- scope :latest_comment_cont, lambda { |msg|
185
- join = <<-SQL
186
- (LEFT OUTER JOIN (
187
- SELECT
188
- comments.*,
189
- row_number() OVER (PARTITION BY comments.article_id ORDER BY comments.id DESC) AS rownum
190
- FROM comments
191
- ) AS latest_comment
192
- ON latest_comment.article_id = article.id
193
- AND latest_comment.rownum = 1
194
- )
195
- SQL
196
- .squish
197
-
198
- joins(join).where("latest_comment.body ILIKE ?", "%#{msg}%")
199
- }
200
-
201
- ransacker :title_type, formatter: lambda { |tuples|
202
- title, type = JSON.parse(tuples)
203
- Arel::Nodes::Grouping.new(
204
- [
205
- Arel::Nodes.build_quoted(title),
206
- Arel::Nodes.build_quoted(type)
207
- ]
208
- )
209
- } do |_parent|
210
- articles = Article.arel_table
211
- Arel::Nodes::Grouping.new(
212
- %i[title type].map do |field|
213
- Arel::Nodes::NamedFunction.new(
214
- 'COALESCE',
215
- [
216
- Arel::Nodes::NamedFunction.new('TRIM', [articles[field]]),
217
- Arel::Nodes.build_quoted('')
218
- ]
219
- )
220
- end
221
- )
222
- end
223
- end
224
-
225
- class StoryArticle < Article
226
- end
227
-
228
- class Recommendation < ApplicationRecord
229
- belongs_to :person
230
- belongs_to :target_person, class_name: 'Person'
231
- belongs_to :article
232
- end
233
-
234
- module Namespace
235
- class Article < ::Article
236
-
237
- end
238
- end
239
-
240
- module Namespace
241
- class Article < ::Article
242
-
243
- end
244
- end
245
-
246
- class Comment < ApplicationRecord
247
- belongs_to :article
248
- belongs_to :person
249
- has_and_belongs_to_many :tags
250
-
251
- default_scope { where(disabled: false) }
252
- end
253
-
254
- class Tag < ApplicationRecord
255
- has_and_belongs_to_many :articles
256
- has_and_belongs_to_many :comments
257
- end
258
-
259
- class Note < ApplicationRecord
260
- belongs_to :notable, polymorphic: true
261
- end
262
-
263
- class RecentNote < ApplicationRecord
264
- DEFAULT_NOTABLE_ID = 1
265
- self.table_name = "notes"
266
- default_scope { where(notable_id: DEFAULT_NOTABLE_ID) }
267
-
268
- belongs_to :notable, polymorphic: true
269
- end
270
-
271
- class Account < ApplicationRecord
272
- belongs_to :agent_account, class_name: "Account"
273
- belongs_to :trade_account, class_name: "Account"
274
- end
275
-
276
- class Address < ApplicationRecord
277
- has_one :organization
278
- end
279
-
280
- class Organization < ApplicationRecord
281
- belongs_to :address
282
- has_many :employees
283
- end
284
-
285
- class Employee < ApplicationRecord
286
- belongs_to :organization
287
- has_one :address, through: :organization
288
- end
289
-
290
- module Schema
291
- def self.create
292
- ActiveRecord::Migration.verbose = false
293
-
294
- ActiveRecord::Schema.define do
295
- create_table :people, force: true do |t|
296
- t.integer :parent_id
297
- t.string :name
298
- t.string :email
299
- t.string :only_search
300
- t.string :only_sort
301
- t.string :only_admin
302
- t.string :new_start
303
- t.string :stop_end
304
- t.integer :salary
305
- t.integer :temperament
306
- t.date :life_start
307
- t.boolean :awesome, default: false
308
- t.boolean :terms_and_conditions, default: false
309
- t.boolean :true_or_false, default: true
310
- t.timestamps null: false
311
- end
312
-
313
- create_table :articles, force: true do |t|
314
- t.integer :person_id
315
- t.string :title
316
- t.text :subject_header
317
- t.text :body
318
- t.string :type
319
- t.boolean :published, default: true
320
- end
321
-
322
- create_table :comments, force: true do |t|
323
- t.integer :article_id
324
- t.integer :person_id
325
- t.text :body
326
- t.boolean :disabled, default: false
327
- end
328
-
329
- create_table :tags, force: true do |t|
330
- t.string :name
331
- end
332
-
333
- create_table :articles_tags, force: true, id: false do |t|
334
- t.integer :article_id
335
- t.integer :tag_id
336
- end
337
-
338
- create_table :comments_tags, force: true, id: false do |t|
339
- t.integer :comment_id
340
- t.integer :tag_id
341
- end
342
-
343
- create_table :notes, force: true do |t|
344
- t.integer :notable_id
345
- t.string :notable_type
346
- t.string :note
347
- end
348
-
349
- create_table :recommendations, force: true do |t|
350
- t.integer :person_id
351
- t.integer :target_person_id
352
- t.integer :article_id
353
- end
354
-
355
- create_table :accounts, force: true do |t|
356
- t.belongs_to :agent_account
357
- t.belongs_to :trade_account
358
- end
359
-
360
- create_table :addresses, force: true do |t|
361
- t.string :city
362
- end
363
-
364
- create_table :organizations, force: true do |t|
365
- t.string :name
366
- t.integer :address_id
367
- end
368
-
369
- create_table :employees, force: true do |t|
370
- t.string :name
371
- t.integer :organization_id
372
- end
373
- end
374
-
375
- 10.times do
376
- person = Person.make
377
- Note.make(notable: person)
378
- 3.times do
379
- article = Article.make(person: person)
380
- 3.times do
381
- article.tags = [Tag.make, Tag.make, Tag.make]
382
- end
383
- Note.make(notable: article)
384
- 10.times do
385
- Comment.make(article: article, person: person)
386
- end
387
- end
388
- end
389
-
390
- Comment.make(
391
- body: 'First post!',
392
- article: Article.make(title: 'Hello, world!')
393
- )
394
- end
395
- end
396
-
397
- module SubDB
398
- class Base < ApplicationRecord
399
- self.abstract_class = true
400
- establish_connection(
401
- adapter: 'sqlite3',
402
- database: ':memory:'
403
- )
404
- end
405
-
406
- class OperationHistory < Base
407
- end
408
-
409
- module Schema
410
- def self.create
411
- s = ::ActiveRecord::Schema.new
412
- s.instance_variable_set(:@connection, SubDB::Base.connection)
413
- s.verbose = false
414
- s.define({}) do
415
- create_table :operation_histories, force: true do |t|
416
- t.string :operation_type
417
- t.integer :people_id
418
- end
419
- end
420
- end
421
- end
422
- end