-
Notifications
You must be signed in to change notification settings - Fork 0
Reimplementación de la gema #14
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
Open
iobaixas
wants to merge
7
commits into
master
Choose a base branch
from
feat/admin-resource
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b93f016
feat: use monkey patch strategy to handle active resource
educifuentes 94759b5
fix: prevent ResourceChainAdaptor from sending empty query string
iobaixas 6b5589b
refactor: removes unused buda specific classes
iobaixas 34130b5
feat: make ResourceClassAdapter require resource to implement specifi…
iobaixas a6c0981
chore: updates README
iobaixas a9d8b5a
fix: rails 6 support
iobaixas 0d8d4c9
style: adds missing line breaks
iobaixas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,6 @@ | |
| Extension of [ActiveResource](https://github.com/rails/activeresource) to allow integration | ||
| with [ActiveAdmin](https://github.com/activeadmin/activeadmin). | ||
|
|
||
| ## Disclaimer | ||
|
|
||
| This gem is work in progress. There is only partial support of ActiveAdmin for now, and the are some specific limitation and requirements that need to be considered when using it. | ||
|
|
||
| Pull Requests with improvements are welcomed :) | ||
|
|
||
| ## Installation | ||
|
|
||
| Add this line to your Rails application's Gemfile: | ||
|
|
@@ -21,167 +15,28 @@ And then execute: | |
|
|
||
| $ bundle | ||
|
|
||
| ## Usage | ||
|
|
||
| This gem provides a base class that extends from ActiveResource and patches some missing functions in both ActiveAdmin and ActiveResource. The patches are executed when the gem is loaded. | ||
|
|
||
| ### ActiveAdminResource::Base | ||
|
|
||
| This is the base class that you need to use to create ActiveAdmin enabled ActiveResource models. You need to extend your model with this base class, and define the minimum fields required by ActiveResource as in the following example: | ||
|
|
||
| ```ruby | ||
| class Market < ActiveAdminResource::Base | ||
| self.site = "http://localhost/api/" | ||
|
|
||
| schema do | ||
| attribute 'id', :string | ||
| attribute 'name', :string | ||
| attribute 'base_currency', :string | ||
| attribute 'quote_currency', :string | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| This base class allows also to perform signed requests using the [Authograph gem](https://github.com/budacom/authograph/). To enable this, the class must implement the class method *self.secret* and return the private key in that method. Additionaly, an id of the requester can be provided by implementing the *self.agent_id* method as the following example shows: | ||
|
|
||
| ```ruby | ||
| class Account < ActiveAdminResource::Base | ||
| self.site = "http://localhost/signed_api/" | ||
|
|
||
| def self.agent_id | ||
| #Return id | ||
| end | ||
|
|
||
| def self.secret | ||
| #Return secret | ||
| end | ||
|
|
||
| schema do | ||
| attribute 'id', :integer | ||
| attribute 'name', :string | ||
| end | ||
| end | ||
|
|
||
| ``` | ||
|
|
||
| ActiveResource has partial support for relations between models. You can specify `has_one` and `has_many`relations just like its is done in ActiveRecord: | ||
|
|
||
| ```ruby | ||
| class Account < ActiveAdminResource::Base | ||
| self.site = "http://localhost/signed_api/" | ||
|
|
||
| has_one :agent_data | ||
| has_many :withdrawals | ||
|
|
||
| def self.agent_id | ||
| #Return id | ||
| end | ||
|
|
||
| def self.secret | ||
| #Return secret | ||
| end | ||
|
|
||
| schema do | ||
| attribute 'id', :integer | ||
| attribute 'name', :string | ||
| end | ||
| end | ||
|
|
||
| ``` | ||
|
|
||
| The difference in how relations are handled is that for the subelement of a model (in this example `AgentData`) the url must include the parent | ||
| url with a symbol indicating the id of the parent record (in this example `:account_id`). | ||
|
|
||
| ```ruby | ||
| class AgentData < ActiveAdminResource::AgentDataBase | ||
| self.site = "http://localhost/signed_api/accounts/:account_id" | ||
|
|
||
| def self.agent_id | ||
| #Return id | ||
| end | ||
|
|
||
| def self.secret | ||
| #Return secret | ||
| end | ||
|
|
||
| schema do | ||
| attribute 'a', :string | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| When accessing the subelement and trying to perform an action, this extra symbol must be included as a parameter to complete the url: | ||
|
|
||
| ```ruby | ||
| Account.find(4).agent_data.update_attributes(a: "asdf", account_id: 4) | ||
| ``` | ||
|
|
||
| ### ActiveAdminResource::AgentDataBase | ||
| Since API quering changes from one ActiveResource integration to another, for this gem to work the proyect's base resource class must implement the following methods: | ||
|
|
||
| An additional base class called `AgentDataBase` is provided to model the special case of an `AgentData` object: | ||
| ### `process_active_admin_collection_query(ransack:, reorder:, page:, per:)` | ||
|
|
||
| ```ruby | ||
| class AgentData < ActiveAdminResource::AgentDataBase | ||
| self.site = "http://localhost/signed_api/" | ||
|
|
||
| def self.agent_id | ||
| #Return id | ||
| end | ||
|
|
||
| def self.secret | ||
| #Return secret | ||
| end | ||
|
|
||
| schema do | ||
| attribute 'a', :string | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| When registering an ActiveAdminResource model with ActiveAdmin some additional considerations must be taken: | ||
|
|
||
| * Batch actions are not supported, so they should be disabled. | ||
| * The *find_collection* method of *controller* must be overriden and return a paginated array as a result. | ||
| * Pagination is supported by providing pagination info in the server response. The currently supported pagination format expect a *meta* field in the response with a dictionary that has the following fields describing the pagination: *total_pages*, *total_count* and *current_page*. | ||
| When a response is requested, the pagination info is stored in *Model.format.pagination_info* and can be used to paginate the info accordingly. | ||
| This method will be called with the following arguments: | ||
| - `ransack`: ransack query activeadmin is trying to apply to collection | ||
| - `reorder`: ordering query activeadmin is trying to apply to collection | ||
| - `page`: page number | ||
| - `per`: results per page | ||
|
|
||
| The method must respond with an instance of `ActiveAdminResource::QueryResult`, that can be built using `ActiveAdminResource::QueryResult.new(collection, total_count, page)`, where: | ||
| - `collection`: an array of resource instances | ||
| - `total_count`: the total count of resources after applying filters (`nil` if not available) | ||
| - `page`: the current page (`nil` if not available) | ||
|
|
||
| The following example shows an ActiveAdminResource model being registered for ActiveAdmin and using the pagination schema explained before. | ||
|
|
||
| ```ruby | ||
|
|
||
| ActiveAdmin.register Account do | ||
| config.batch_actions = false | ||
|
|
||
| filter :names, as: :string, label: "Names" | ||
| filter :surnames, as: :string, label: "Surnames" | ||
|
|
||
| controller do | ||
| def find_collection | ||
| default_per_page = 20 | ||
| per_page = params.fetch(:per_page, default_per_page) | ||
| query_params = params.fetch(:q, nil) | ||
| search_params = query_params.nil? ? {} : query_params.permit!.to_h | ||
| @search = OpenStruct.new(search_params.merge(conditions: [])) | ||
| result = Account.find(:all, params: { | ||
| order: params.fetch(:order, nil), | ||
| page: params.fetch(:page, 1), | ||
| per: per_page, | ||
| search: search_params | ||
| }) | ||
| pagination_info = Account.format.pagination_info | ||
| offset = (pagination_info["current_page"] - 1) * per_page | ||
| Kaminari.paginate_array(result, limit: result.count, offset: offset, total_count: pagination_info["total_count"]) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| ``` | ||
| ### `process_active_admin_resource_query(_id)` (optional) | ||
|
|
||
| If this method is defined, it will be called instead if `find` | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| ### Limitations and Poorly tested cases | ||
| ## Limitations | ||
|
|
||
| As explained before, there are several limitations and poorly tested cases in the current version: | ||
| **WARNING! There are several limitations and no tests, so use with caution** | ||
|
|
||
| * Batch actions are not supported | ||
| * POST, PUT and DELETE actions are not tested well and may have issues | ||
| * There is no way of telling activeadmin when to allow sorting or no | ||
| * There is no support for forms | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| require 'active_admin_resource/base' | ||
| require 'active_admin_resource/associations' | ||
| require 'active_admin_resource/gem_adaptors' | ||
| require 'active_admin_resource/agent_data_base' | ||
| require 'active_admin_resource/query_result' | ||
| require 'active_admin_resource/resource' | ||
| require 'active_admin_resource/resource_column' | ||
| require 'active_admin_resource/resource_class_adapter' | ||
| require 'active_admin_resource/resource_chain_helper' | ||
| require 'active_admin_resource/active_admin/namespace_patch' | ||
| require 'active_admin_resource/railtie' |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module ActiveAdminResource | ||
| module ActiveAdmin | ||
| module NamespacePatch | ||
| def find_or_build_resource(resource_class, options) | ||
| if resource_class < ActiveResource::Base | ||
| resources.add ActiveAdminResource::Resource.new(self, resource_class, options) | ||
| else | ||
| resources.add ::ActiveAdmin::Resource.new(self, resource_class, options) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
project's*