Skip to content

Active Admin + Active Resource: to use a REST API in place of a local database as data source

License

Notifications You must be signed in to change notification settings

thiagodiniz/activeadmin_active_resource

 
 

Repository files navigation

Active Admin + Active Resource

gem version gem downloads linters specs specs2

An Active Admin plugin to use a REST API data source in place of a local database using Active Resource.

NOTICE: currently some Active Admin features don't work as expected:

  • Filters are partially supported (see example)
  • Some form fields must be configured explicitly (like the associations)
  • Comments are not supported

Please ⭐ if you like it.

Install

  • Add to your project's Gemfile (with Active Admin installed): gem 'activeadmin_active_resource'
  • Execute bundle
  • Disable comments in Active Admin config initializer (config.comments = false)

Examples

Please take a look at the examples folder:

  • a Rails6 application with Active Admin and this component, see here;
  • a Rails6 API application used as data source, see here;
  • a Roda API application used as data source, see here.

For more examples check the specs.

Basic instructions:

  • Post model:
class Post < ActiveResource::Base
  self.site = 'http://localhost:3000'  # API url: another Rails project, a REST API, etc.

  self.schema = {  # Fields must be declared explicitly
    id: :integer,
    title: :string,
    description: :text,
    author_id: :integer,
    category: :string,
    dt: :datetime,
    position: :float,
    published: :boolean,
    created_at: :datetime,
    updated_at: :datetime,
  }
end
  • Post admin config:
ActiveAdmin.register Post do
  filter :title_cont  # Ransack postfixes required (_eq, _cont, etc.)

  controller do
    def permitted_params
      params.permit!  # Permit all just for testing
    end
  end

  form do |f|
    f.inputs do
      f.input :id, as: :hidden unless f.object.new_record?  # Required
      f.input :title
      # ... other fields
    end
    f.actions
  end
end
  • Ransack options here
  • Rails API index example with Ransack and Kaminari:
  after_action :set_pagination, only: [:index]

  def index
    per_page = params[:per_page].to_i
    per_page = 15 if per_page < 1
    @posts = Post.ransack( params[:q] ).result.order( params[:order] ).page( params[:page].to_i ).per( per_page )
  end

  def set_pagination
    headers['Pagination-Limit'] = @posts.limit_value.to_s
    headers['Pagination-Offset'] = @posts.offset_value.to_s
    headers['Pagination-TotalCount'] = @posts.total_count.to_s
  end

Notes

If you create a new rails project don't use --skip-active-record.

Do you like it? Star it!

If you use this component just star it. A developer is more motivated to improve a project when there is some interest. My other Active Admin components.

Or consider offering me a coffee, it's a small thing but it is greatly appreciated: about me.

Contributors

License

The gem is available as open-source under the terms of the MIT.

About

Active Admin + Active Resource: to use a REST API in place of a local database as data source

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 87.0%
  • HTML 9.9%
  • JavaScript 1.5%
  • CSS 1.3%
  • Other 0.3%