Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can use namespaced model #132

Merged
merged 9 commits into from
Mar 24, 2020
Merged

Can use namespaced model #132

merged 9 commits into from
Mar 24, 2020

Conversation

Eth3rnit3
Copy link
Contributor

I will create this pull-request if it can make the gem evolve a bit.

Namespaced models

I encountered a problem when I wanted to use a target model under module. example Entity::House and this model must have a route in this format /api/v1/houses I know it's a bit peculiar but the only way I found to do it was to define my routes in the following way

namespace :api do
  namespace :v1 do
    notify_to :applicants, api_mode: true, controller: '/activity_notification/notifications_api', target_type: 'entity/houses'
    subscribed_by :applicants, api_mode: true, controller: '/activity_notification/subscriptions_api', target_type: 'entity/houses'
  end
end

Nested patterns are problematic in set_target methods because the '/' prevents target_type.to_s.to_resource_name from working properly.
That's why I added a regex that keeps only what comes after the last `'/'' symbol in each transformation.

thus

"#{'entity/houses'.to_resource_name[/([^\/]+)$/]}_id"
# => "house_id"

After create subscription

After creating a new subscription through the api, I receive this error

> No route matches {:action=>"show", :applicant_id=>"5dd290fea70bf278058d998e", :controller=>"activity_notification/subscriptions_api", :id=>#<ActivityNotification::Subscription _id: 5e62dc16d19554b27ff39632, created_at: 2020-03-06 23:26:14 UTC, updated_at: 2020-03-06 23:26:14 UTC, target_id: "5dd290fea70bf278058d998e", target_type: "Hermes::Applicant", key: "housing_offer.new.eligible", subscribing: true, subscribing_to_email: true, subscribed_at: 2020-03-06 23:26:14 UTC, unsubscribed_at: nil, subscribed_to_email_at: 2020-03-06 23:26:14 UTC, unsubscribed_to_email_at: nil, optional_targets: {"subscribing_to_slack"=>false, "unsubscribed_to_slack_at"=>2020-03-06 23:26:14 UTC}, _type: "ActivityNotification::Subscription">, :target_type=>"hermes/applicants"}

The subscription is well created, I can find it afterwards and update it without any problem, it's only the render of the create method that poses a problem.

Fix with development...Eth3rnit3:master#diff-537d9b369239b60813ca53c7855b1593R47

Minimal change

While browsing this file I realized that 2 methods were doing the same thing except that one was converting the result to a symbol, I just made the first one call the second one.

development...Eth3rnit3:master#diff-9797a15e0073d37a0b5bfa1df2b2f692R485

@simukappu
Copy link
Owner

Hi @Eth3rnit3

Great! Thank you for your contribution, but can I ask one more thing?

  • Add documentation to use namespaced target model (as you mentioned about routing configuration)

Thank you!

@Eth3rnit3
Copy link
Contributor Author

Hello @simukappu I have just updated the setup section of the documentation by giving an example of integration with a submodule model.

https://github.com/simukappu/activity_notification/pull/132/files#diff-614f5c7f5ec0a8f955f030135ec52edcR346

Have a nice day

@simukappu
Copy link
Owner

Cool. Thank you!

@simukappu simukappu force-pushed the development branch 3 times, most recently from 713969b to ccd84f7 Compare March 15, 2020 15:25
@simukappu simukappu merged commit 6bba7c2 into simukappu:development Mar 24, 2020
simukappu pushed a commit that referenced this pull request Aug 3, 2020
* Enable to use namespaced target in other scope like target = Namespace::Model scope api/v1/models/model_id/notifications
* Fix after create error of subscription controller
* Update documentation about namespaced target model
simukappu pushed a commit that referenced this pull request Aug 10, 2020
* Enable to use namespaced target in other scope like target = Namespace::Model scope api/v1/models/model_id/notifications
* Fix after create error of subscription controller
* Update documentation about namespaced target model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants