|
334 | 334 | t.string :name
|
335 | 335 | end
|
336 | 336 |
|
| 337 | + create_table :painters, force: true do |t| |
| 338 | + t.string :name |
| 339 | + |
| 340 | + t.timestamps null: false |
| 341 | + end |
| 342 | + |
| 343 | + create_table :paintings, force: true do |t| |
| 344 | + t.string :title |
| 345 | + t.string :category |
| 346 | + t.belongs_to :painter |
| 347 | + |
| 348 | + t.timestamps null: false |
| 349 | + end |
| 350 | + |
| 351 | + create_table :collectors, force: true do |t| |
| 352 | + t.string :name |
| 353 | + t.belongs_to :painting |
| 354 | + end |
| 355 | + |
| 356 | + # special cases |
337 | 357 | create_table :storages, force: true do |t|
|
338 | 358 | t.string :token, null: false
|
339 | 359 | t.string :name
|
@@ -433,6 +453,11 @@ class Person < ActiveRecord::Base
|
433 | 453 | has_one :author_detail
|
434 | 454 |
|
435 | 455 | has_and_belongs_to_many :books, join_table: :book_authors
|
| 456 | + has_and_belongs_to_many :not_banned_books, -> { |
| 457 | + merge(Book.not_banned) |
| 458 | + }, |
| 459 | + class_name: 'Book', |
| 460 | + join_table: :book_authors |
436 | 461 |
|
437 | 462 | has_many :even_posts, -> { where('posts.id % 2 = 0') }, class_name: 'Post', foreign_key: 'author_id'
|
438 | 463 | has_many :odd_posts, -> { where('posts.id % 2 = 1') }, class_name: 'Post', foreign_key: 'author_id'
|
@@ -619,14 +644,18 @@ class Book < ActiveRecord::Base
|
619 | 644 | has_many :approved_book_comments, -> { where(approved: true) }, class_name: "BookComment"
|
620 | 645 |
|
621 | 646 | has_and_belongs_to_many :authors, join_table: :book_authors, class_name: "Person"
|
| 647 | + |
| 648 | + scope :not_banned, -> { |
| 649 | + where(banned: false) |
| 650 | + } |
622 | 651 | end
|
623 | 652 |
|
624 | 653 | class BookComment < ActiveRecord::Base
|
625 | 654 | belongs_to :author, class_name: 'Person', foreign_key: 'author_id'
|
626 | 655 | belongs_to :book
|
627 | 656 |
|
628 | 657 | def self.for_user(current_user)
|
629 |
| - records = self |
| 658 | + records = self.all |
630 | 659 | # Hide the unapproved comments from people who are not book admins
|
631 | 660 | unless current_user && current_user.book_admin
|
632 | 661 | records = records.where(approved: true)
|
@@ -688,8 +717,8 @@ class Category < ActiveRecord::Base
|
688 | 717 | class Picture < ActiveRecord::Base
|
689 | 718 | belongs_to :imageable, polymorphic: true
|
690 | 719 |
|
691 |
| - # belongs_to :document, -> { where( pictures: { imageable_type: 'Document' } ).includes( :pictures ) }, foreign_key: 'imageable_id' |
692 |
| - # belongs_to :product, -> { where( pictures: { imageable_type: 'Product' } ).includes( :pictures ) }, foreign_key: 'imageable_id' |
| 720 | + belongs_to :document, -> { where( pictures: { imageable_type: 'Document' } ).eager_load( :pictures ) }, foreign_key: 'imageable_id' |
| 721 | + belongs_to :product, -> { where( pictures: { imageable_type: 'Product' } ).eager_load( :pictures ) }, foreign_key: 'imageable_id' |
693 | 722 | end
|
694 | 723 |
|
695 | 724 | class Vehicle < ActiveRecord::Base
|
@@ -797,6 +826,19 @@ class Widget < ActiveRecord::Base
|
797 | 826 | class Robot < ActiveRecord::Base
|
798 | 827 | end
|
799 | 828 |
|
| 829 | +class Painter < ActiveRecord::Base |
| 830 | + has_many :paintings |
| 831 | +end |
| 832 | + |
| 833 | +class Painting < ActiveRecord::Base |
| 834 | + belongs_to :painter |
| 835 | + has_many :collectors |
| 836 | +end |
| 837 | + |
| 838 | +class Collector < ActiveRecord::Base |
| 839 | + belongs_to :painting |
| 840 | +end |
| 841 | + |
800 | 842 | ### CONTROLLERS
|
801 | 843 | class SessionsController < ActionController::Base
|
802 | 844 | include JSONAPI::ActsAsResourceController
|
@@ -1014,6 +1056,9 @@ class ExpenseEntriesController < JSONAPI::ResourceController
|
1014 | 1056 |
|
1015 | 1057 | class IsoCurrenciesController < JSONAPI::ResourceController
|
1016 | 1058 | end
|
| 1059 | + |
| 1060 | + class PaintersController < JSONAPI::ResourceController |
| 1061 | + end |
1017 | 1062 | end
|
1018 | 1063 |
|
1019 | 1064 | module V6
|
@@ -1349,6 +1394,11 @@ def title=(title)
|
1349 | 1394 | records.where(title: values.first['title'])
|
1350 | 1395 | }
|
1351 | 1396 |
|
| 1397 | + filter 'tags.name' |
| 1398 | + |
| 1399 | + filter 'comments.author.name' |
| 1400 | + filter 'comments.tags.name' |
| 1401 | + |
1352 | 1402 | def self.updatable_fields(context)
|
1353 | 1403 | super(context) - [:author, :subject]
|
1354 | 1404 | end
|
@@ -1508,7 +1558,7 @@ class CraterResource < JSONAPI::Resource
|
1508 | 1558 |
|
1509 | 1559 | filter :description, apply: -> (records, value, options) {
|
1510 | 1560 | fail "context not set" unless options[:context][:current_user] != nil && options[:context][:current_user] == $test_user
|
1511 |
| - records.where(concat_table_field(options[:table_alias], :description) => value) |
| 1561 | + records.where(concat_table_field(options[:related_alias], :description) => value) |
1512 | 1562 | }
|
1513 | 1563 |
|
1514 | 1564 | def self.verify_key(key, context = nil)
|
@@ -1545,7 +1595,16 @@ class CategoryResource < JSONAPI::Resource
|
1545 | 1595 | class PictureResource < JSONAPI::Resource
|
1546 | 1596 | attribute :name
|
1547 | 1597 | has_one :imageable, polymorphic: true
|
1548 |
| - # has_one :imageable, polymorphic: true, polymorphic_relations: [:document, :product] |
| 1598 | + |
| 1599 | + filter 'imageable.name', perform_joins: true, apply: -> (records, value, options) { |
| 1600 | + joins = options[:joins] |
| 1601 | + relationship = _relationship(:imageable) |
| 1602 | + or_parts = relationship.polymorphic_relations.collect do |relation| |
| 1603 | + table_alias = joins["imageable[#{relation}]"][:alias] |
| 1604 | + "#{concat_table_field(table_alias, "name")} = '#{value.first}'" |
| 1605 | + end |
| 1606 | + records.where(or_parts.join(' OR ')) |
| 1607 | + } |
1549 | 1608 | end
|
1550 | 1609 |
|
1551 | 1610 | class DocumentResource < JSONAPI::Resource
|
@@ -1744,33 +1803,15 @@ class AuthorResource < JSONAPI::Resource
|
1744 | 1803 | model_name 'Person'
|
1745 | 1804 | attributes :name
|
1746 | 1805 |
|
1747 |
| - has_many :books, inverse_relationship: :authors, |
1748 |
| - custom_methods: { |
1749 |
| - apply_join: -> (options) { |
1750 |
| - relationship = options[:relationship] |
1751 |
| - relation_name = relationship.relation_name(options[:options]) |
1752 |
| - |
1753 |
| - records = options[:records].joins(relation_name).references(relation_name) |
1754 |
| - |
1755 |
| - unless options[:context][:current_user].try(:book_admin) |
1756 |
| - records = records.where("#{relation_name}.banned" => false) |
1757 |
| - end |
1758 |
| - records |
1759 |
| - } |
1760 |
| - } |
| 1806 | + has_many :books, inverse_relationship: :authors, relation_name: -> (options) { |
| 1807 | + if options[:context][:current_user].try(:book_admin) |
| 1808 | + :books |
| 1809 | + else |
| 1810 | + :not_banned_books |
| 1811 | + end |
| 1812 | + } |
1761 | 1813 |
|
1762 | 1814 | has_many :book_comments
|
1763 |
| - |
1764 |
| - def records_for(rel_name) |
1765 |
| - records = _model.public_send(rel_name) |
1766 |
| - if rel_name == :books |
1767 |
| - # Hide indirect access to banned books unless current user is a book admin |
1768 |
| - unless context[:current_user].try(:book_admin) |
1769 |
| - records = records.where(banned: false) |
1770 |
| - end |
1771 |
| - end |
1772 |
| - return records |
1773 |
| - end |
1774 | 1815 | end
|
1775 | 1816 |
|
1776 | 1817 | class BookResource < JSONAPI::Resource
|
@@ -1804,6 +1845,7 @@ class BookResource < JSONAPI::Resource
|
1804 | 1845 | :book_comments
|
1805 | 1846 | end
|
1806 | 1847 |
|
| 1848 | + # Using an inner join here, which is different than the new default left_join |
1807 | 1849 | return records.joins(relation).references(relation).where('book_comments.id' => value)
|
1808 | 1850 | }
|
1809 | 1851 |
|
@@ -1944,6 +1986,36 @@ class AuthorDetailResource < JSONAPI::Resource
|
1944 | 1986 | attributes :author_stuff
|
1945 | 1987 | end
|
1946 | 1988 |
|
| 1989 | + class PaintingResource < JSONAPI::Resource |
| 1990 | + model_name 'Painting' |
| 1991 | + attributes :title, :category #, :collector_roster |
| 1992 | + has_one :painter |
| 1993 | + has_many :collectors |
| 1994 | + |
| 1995 | + filter :title |
| 1996 | + filter :category |
| 1997 | + filter :collectors |
| 1998 | + |
| 1999 | + def collector_roster |
| 2000 | + collectors.map(&:name) |
| 2001 | + end |
| 2002 | + end |
| 2003 | + |
| 2004 | + class CollectorResource < JSONAPI::Resource |
| 2005 | + attributes :name |
| 2006 | + has_one :painting |
| 2007 | + end |
| 2008 | + |
| 2009 | + class PainterResource < JSONAPI::Resource |
| 2010 | + model_name 'Painter' |
| 2011 | + attributes :name |
| 2012 | + has_many :paintings |
| 2013 | + |
| 2014 | + filter :name, apply: lambda { |records, value, options| |
| 2015 | + records.where('name LIKE ?', value) |
| 2016 | + } |
| 2017 | + end |
| 2018 | + |
1947 | 2019 | class PersonResource < PersonResource; end
|
1948 | 2020 | class PostResource < PostResource; end
|
1949 | 2021 | class TagResource < TagResource; end
|
@@ -2262,23 +2334,18 @@ def show
|
2262 | 2334 | module Api
|
2263 | 2335 | class BoxResource < JSONAPI::Resource
|
2264 | 2336 | has_many :things
|
| 2337 | + |
| 2338 | + filter 'things.things.name' |
| 2339 | + filter 'things.name' |
2265 | 2340 | end
|
2266 | 2341 |
|
2267 | 2342 | class ThingResource < JSONAPI::Resource
|
2268 | 2343 | has_one :box
|
2269 | 2344 | has_one :user
|
2270 | 2345 |
|
2271 |
| - has_many :things, |
2272 |
| - custom_methods: { |
2273 |
| - apply_join: -> (options) { |
2274 |
| - table_alias = "aliased_#{options[:table_alias]}" |
2275 |
| - options[:table_alias] = table_alias |
2276 |
| - |
2277 |
| - join_stmt = "LEFT OUTER JOIN related_things related_things_#{table_alias} ON related_things_#{table_alias}.from_id = things.id LEFT OUTER JOIN things \"#{table_alias}\" ON \"#{table_alias}\".id = related_things_#{table_alias}.to_id" |
| 2346 | + has_many :things |
2278 | 2347 |
|
2279 |
| - return options[:records].joins(join_stmt) |
2280 |
| - } |
2281 |
| - } |
| 2348 | + filter 'things.things.name' |
2282 | 2349 | end
|
2283 | 2350 |
|
2284 | 2351 | class UserResource < JSONAPI::Resource
|
|
0 commit comments