activeadmin_active_resource 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5176bbe601d9aa99e39fceffb8e970d2968daacf
4
- data.tar.gz: 005f7938c408c007a1cec4f2308445e2e076c22b
2
+ SHA256:
3
+ metadata.gz: d0f390fdd3d4ec7772412e134b3e2ed01a103e27197ddaeae46addcdc6cdc9fe
4
+ data.tar.gz: 38aa9bfde64b0e486e1b4328cc9aaa33045a2de8294f2fcb2c01ec21d951aaed
5
5
  SHA512:
6
- metadata.gz: fa97f8f4b8882a288d5618c71bf5d750ee4a5732b537cef208d23404f483e2089a095f033a2892cb6a0b950a8a05d8d655a3e305f9cc1d5956e4dd0c7b227890
7
- data.tar.gz: c5419a948a7b0b086cd70e05a0a0b27630bf14eb48323073674e869df0de9bca48809da4b15eb6d97ccc5d90e2535e9dd7690f006a0f65b01b299f54f7a40d3a
6
+ metadata.gz: 25c11edf28aed4ad54396002dd9574be12567bcec267ca78d56df95a543ca417d7fe87b24aa13b851271fcabfca9fc6b11458b7423788cdd64cb4ae0165cbd43
7
+ data.tar.gz: dd78d7fd966f9d35160a82f7e68ee6636b77dc4e839dc89eb22b89ba4f0a7b133b38e08a25f32a5c91597edc417b8c6c109ab43983e54d3273ea0eb5ae312f2e
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # ActiveAdmin Active Resource [![Gem Version](https://badge.fury.io/rb/activeadmin_active_resource.svg)](https://badge.fury.io/rb/activeadmin_active_resource)
1
+ # ActiveAdmin + Active Resource [![Gem Version](https://badge.fury.io/rb/activeadmin_active_resource.svg)](https://badge.fury.io/rb/activeadmin_active_resource)
2
2
 
3
- Active Admin + [Active Resource](https://github.com/rails/activeresource): to use a REST API in place of a local database as data source.
3
+ An ActiveAdmin plugin to use a REST API in place of a local database as data source using [Active Resource](https://github.com/rails/activeresource).
4
4
 
5
5
  WARNING: this component is a Beta version, some Active Admin functionalities don't work as expected:
6
6
 
@@ -62,6 +62,24 @@ end
62
62
 
63
63
  - Ransack options [here](https://github.com/activerecord-hackery/ransack#search-matchers)
64
64
 
65
+ - Rails API index example with Ransack and Kaminari:
66
+
67
+ ```rb
68
+ after_action :set_pagination, only: [:index]
69
+
70
+ def index
71
+ per_page = params[:per_page].to_i
72
+ per_page = 15 if per_page < 1
73
+ @posts = Post.ransack( params[:q] ).result.order( params[:order] ).page( params[:page].to_i ).per( per_page )
74
+ end
75
+
76
+ def set_pagination
77
+ headers['Pagination-Limit'] = @posts.limit_value.to_s
78
+ headers['Pagination-Offset'] = @posts.offset_value.to_s
79
+ headers['Pagination-TotalCount'] = @posts.total_count.to_s
80
+ end
81
+ ```
82
+
65
83
  ## Notes
66
84
 
67
85
  If you create a new rails project don't use *--skip-active-record*
@@ -14,20 +14,20 @@ end
14
14
  self.collection_parser = ActiveAdmin::ActiveResource::Results
15
15
 
16
16
  class << self
17
- prepend( FindExt = Module.new do
18
- def find( *arguments )
17
+ prepend(FindExt = Module.new do
18
+ def find(*arguments)
19
19
  # First argument an array -> batch action
20
- if arguments.count > 0 && arguments[0].is_a?( Array )
20
+ if arguments.count > 0 && arguments[0].is_a?(Array)
21
21
  ret = []
22
22
  arguments[0].each do |id|
23
- ret << find( id )
23
+ ret << find(id)
24
24
  end
25
25
  ret.compact
26
26
  else
27
27
  super
28
28
  end
29
29
  end
30
- end )
30
+ end)
31
31
 
32
32
  def _ransackers
33
33
  {}
@@ -40,24 +40,25 @@ end
40
40
 
41
41
  # -> http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-columns
42
42
  def columns
43
- @columns ||= self.known_attributes.map { |col| OpenStruct.new( name: col ) }
43
+ @columns ||= self.known_attributes.map { |col| OpenStruct.new(name: col) }
44
44
  end
45
45
 
46
- def find_all( options = {} )
46
+ def find_all(options = {})
47
47
  prefix_options, query_options = split_options(options[:params])
48
+ query_options[:limit] = query_options[:per_page]
48
49
  path = collection_path(prefix_options, query_options)
49
50
  @connection_response = connection.get(path, headers)
50
- instantiate_collection( (format.decode( @connection_response.body ) || []), query_options, prefix_options )
51
+ instantiate_collection((format.decode( @connection_response.body ) || []), query_options, prefix_options)
51
52
  end
52
53
 
53
54
  # -> http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
54
- def find_by( arg, *args )
55
- arg && arg['id'] ? self.find( arg['id'] ) : self.find( :first, arg )
55
+ def find_by(arg, *args)
56
+ arg && arg['id'] ? self.find(arg['id']) : self.find(:first, arg)
56
57
  end
57
58
 
58
59
  # -> http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-inheritance_column
59
60
  def inheritance_column
60
- ( @inheritance_column ||= nil ) || 'type'
61
+ (@inheritance_column ||= nil) || 'type'
61
62
  end
62
63
 
63
64
  # -> http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-quoted_table_name
@@ -65,46 +66,50 @@ end
65
66
  @quoted_table_name ||= "\"#{self.to_s.tableize}\""
66
67
  end
67
68
 
68
- def page( page )
69
+ def page(page)
69
70
  @page = page.to_i
70
71
  @page = 1 if @page < 1
71
72
  self
72
73
  end
73
74
 
74
- def per( page_count )
75
- @page_count = page_count.to_i
75
+ def per(page_count)
76
+ @per_page = page_count.to_i
76
77
  results
77
78
  end
78
79
 
79
- def ransack( params = {}, options = {} )
80
+ def ransack(params = {}, options = {})
80
81
  @ransack_params = params.blank? ? {} : params.permit!.to_h
81
- OpenStruct.new( conditions: {}, object: OpenStruct.new( klass: self ), result: self )
82
+ OpenStruct.new(conditions: {}, object: OpenStruct.new(klass: self), result: self)
82
83
  end
83
84
 
84
85
  # -> http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflect_on_all_associations
85
- def reflect_on_all_associations( macro = nil )
86
+ def reflect_on_all_associations(macro = nil)
86
87
  []
87
88
  end
88
89
 
89
90
  # -> http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-reorder
90
- def reorder( sql )
91
+ def reorder(sql)
91
92
  @order = sql
92
93
  self
93
94
  end
94
95
 
95
96
  def results
96
- results = find_all params: {page: @page, per_page: @page_count, order: @order, q: @ransack_params}
97
+ results = find_all params: { page: @page, per_page: @per_page, order: @order, q: @ransack_params }
98
+ decorate_with_pagination_data(results)
99
+ end
100
+
101
+ def decorate_with_pagination_data(results)
97
102
  results.current_page = @page
98
- results.limit_value = @connection_response['pagination-limit'].to_i
99
- results.total_count = @connection_response['pagination-totalcount'].to_i
100
- results.total_pages = ( results.total_count.to_f / results.limit_value ).ceil
103
+ results.limit_value = (@connection_response['pagination-limit'] || @per_page).to_i
104
+ results.total_count = (@connection_response['pagination-totalcount'] || results.count).to_i
105
+ results.total_pages = results.limit_value > 0 ? (results.total_count.to_f / results.limit_value).ceil : 1
101
106
  results
102
107
  end
103
108
  end
104
109
  end
105
110
 
106
111
  ::ActiveResource::Connection.class_eval do
107
- def quote_column_name( column_name )
112
+ def quote_column_name(column_name)
108
113
  column_name
109
114
  end
110
115
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveAdmin
2
2
  module ActiveResource
3
- VERSION = '0.1.2'
3
+ VERSION = '0.1.3'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin_active_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Roccoberton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-04 00:00:00.000000000 Z
11
+ date: 2018-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeadmin
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  version: '0'
76
76
  requirements: []
77
77
  rubyforge_project:
78
- rubygems_version: 2.6.14
78
+ rubygems_version: 2.7.8
79
79
  signing_key:
80
80
  specification_version: 4
81
81
  summary: Active Resource for ActiveAdmin