Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
179 changes: 17 additions & 162 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

project's*


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`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of*


### 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
4 changes: 4 additions & 0 deletions active_admin_resource.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ Gem::Specification.new do |s|
s.add_dependency 'money', '~> 6.6'
s.add_dependency 'formtastic'
s.add_development_dependency 'pry'

s.add_development_dependency 'rails', '~> 6.1.7.4'
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rspec-rails'
end
Binary file added lib/.DS_Store
Binary file not shown.
10 changes: 6 additions & 4 deletions lib/active_admin_resource.rb
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 added lib/active_admin_resource/.DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions lib/active_admin_resource/active_admin/namespace_patch.rb
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
20 changes: 0 additions & 20 deletions lib/active_admin_resource/agent_data_base.rb

This file was deleted.

51 changes: 0 additions & 51 deletions lib/active_admin_resource/associations.rb

This file was deleted.

Loading