Skip to content

apply_filter and apply_sort respects :delegate option of attribute#975

Merged
lgebhardt merged 1 commit intoJSONAPI-Resources:masterfrom
senid231:make-apply-filter-respect-attribute-delegate
May 5, 2017
Merged

apply_filter and apply_sort respects :delegate option of attribute#975
lgebhardt merged 1 commit intoJSONAPI-Resources:masterfrom
senid231:make-apply-filter-respect-attribute-delegate

Conversation

@senid231
Copy link
Copy Markdown
Contributor

@senid231 senid231 commented Feb 7, 2017

Allows to sort and filter by delegated attribute without additional patching.
For example request like

get '/posts', params: {
    filter: {name: 'some_title'}, 
    sort: '-name', 
    fields: {posts: 'name'}
}

for such resource

class PostResource < JSONAPI::Resource
  model_name 'Post'

  attribute :name, delegate: :title
  attribute :body

  filter :name
end

will responds successfully with correct data.

@senid231
Copy link
Copy Markdown
Contributor Author

senid231 commented Feb 8, 2017

maybe we should also change keys in model_error_messages

@senid231
Copy link
Copy Markdown
Contributor Author

@lgebhardt, @dgeb, @barelyknown, what do you think about this?

@hidde-jan
Copy link
Copy Markdown
Contributor

The fact that the two code changes are exactly the same shows that there needs to be a better solution to mapping resource attributes to model attributes and vice versa. Like @senid231 mentions, a proper solution should probably also take care of validation messages (and the corresponding pointers). Something on a class level like

class ProductResource < JSONAPI::Resource
  attribute :name, delegate: :title
  attribute :price

  def self.model_attribute_name(resource_attribute)
    # :name => :title | :price => :price
  end

  def self.resource_attribute_name(model_attribute)
    # :title => :name | :price => :price
  end
end

@senid231 senid231 force-pushed the make-apply-filter-respect-attribute-delegate branch from 6e773fd to 49f0aee Compare April 19, 2017 16:18
@senid231
Copy link
Copy Markdown
Contributor Author

rebased and dried
@lgebhardt please take a look

@lgebhardt lgebhardt merged commit 00ffaf5 into JSONAPI-Resources:master May 5, 2017
@lgebhardt
Copy link
Copy Markdown
Contributor

@senid231 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants