activeadmin_active_resource 0.2.4 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90cbc4a293441580fae9b56c4ca2e5174df2c2e1db140a1e06be080e214af5e1
4
- data.tar.gz: fffc4c46e2aa3c352d8d8e94fa0f6adb13b16ee2c094941484b5e7b4246c0981
3
+ metadata.gz: 99217ffe9d6e57fd4eda0bd713c760eafb6bf3418765e12ad0b5a76d8bc002cd
4
+ data.tar.gz: 2b41192fec46788df9075ec0d3cdff979f500b25d07e03158566b7b19a05e6f8
5
5
  SHA512:
6
- metadata.gz: 7d17626dfce7aec903b32dc299856397bc01cfb92ec93ca4d133e46c52224a2bbf4527da5f64c157e63c1c8cb8ac125ff8a0f8215caebc1fed085eac2be5eccd
7
- data.tar.gz: 92638fb4870396b7824eeefb47035285517b4f689a9416e2cd5ada29b9ef7d58d45eadb99c36ce0cdcc7b78ea731ba4d2babc7abef02bae1cf5f57c3b719e7e7
6
+ metadata.gz: 47692d3c049165f2e7b92bc272d7c555fdc04a69230be34eb6455c1c8b04ab4bc733487332752ba961b1cdcf23dfb9b3308083ce94a3615916ab827415e726a7
7
+ data.tar.gz: a1ea9e3409e76431f0a67f197ed54a7d7abbd1fdfd8978c3c4c89519f5fcdd9e47daff79e1b08d8dfa920b23200f8e7d3ca64264e62638c180e451ff815e8c9c
data/README.md CHANGED
@@ -3,10 +3,10 @@
3
3
 
4
4
  An Active Admin plugin to use a REST API data source in place of a local database using [Active Resource](https://github.com/rails/activeresource).
5
5
 
6
- NOTICE: currently some Active Admin features don't work as expected:
7
- - Filters: partially supported (see example)
8
- - Form fields: some fields must be configured explicitly
9
- - Comments: not supported
6
+ **NOTICE**: currently some Active Admin features don't work as expected:
7
+ - Filters are partially supported (see example)
8
+ - Some form fields must be configured explicitly (like the associations)
9
+ - Comments are not supported
10
10
 
11
11
  ## Install
12
12
  - Add to your project's Gemfile (with Active Admin installed): `gem 'activeadmin_active_resource'`
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAdmin
4
+ module ActiveResource
5
+ module Base
6
+ attr_writer :inheritance_column
7
+
8
+ # ref: https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/ClassMethods.html#method-i-column_for_attribute
9
+ def column_for_attribute(name)
10
+ # => ActiveRecord::ConnectionAdapters::Column
11
+ col_name = name.to_s
12
+ send(:class).columns.find { |col| col.name == col_name }
13
+ end
14
+
15
+ module ClassMethods
16
+ prepend(FindExt = Module.new do
17
+ def find(*arguments)
18
+ # First argument an array -> batch action
19
+ if arguments.count > 0 && arguments[0].is_a?(Array)
20
+ ret = []
21
+ arguments[0].each do |id|
22
+ ret << find(id)
23
+ end
24
+ ret.compact
25
+ else
26
+ super
27
+ end
28
+ end
29
+ end)
30
+
31
+ def _ransackers
32
+ {}
33
+ end
34
+
35
+ # ref: https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-content_columns
36
+ def content_columns
37
+ @content_columns ||= columns.reject do |c|
38
+ # c.name == primary_key || # required to update enities
39
+ c.name == inheritance_column ||
40
+ c.name.end_with?("_id") ||
41
+ c.name.end_with?("_count")
42
+ end
43
+ end
44
+
45
+ # ref: http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-column_names
46
+ def column_names
47
+ @column_names ||= columns.map(&:name)
48
+ end
49
+
50
+ # ref: http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-columns
51
+ def columns
52
+ # => array of ActiveRecord::ConnectionAdapters::Column
53
+ @columns ||= begin
54
+ schema.map do |name, type|
55
+ col_name = name.to_s
56
+ col_type = type.to_sym
57
+ col_type = :hidden if col_name == 'id'
58
+ OpenStruct.new(name: col_name, type: col_type)
59
+ end
60
+ end
61
+ end
62
+
63
+ def find_all(options = {})
64
+ prefix_options, query_options = split_options(options[:params])
65
+ query_options[:limit] = query_options[:per_page]
66
+ path = collection_path(prefix_options, query_options)
67
+ @connection_response = connection.get(path, headers)
68
+ instantiate_collection((format.decode( @connection_response.body ) || []), query_options, prefix_options)
69
+ end
70
+
71
+ # -> http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
72
+ def find_by(arg, *_args)
73
+ arg && arg['id'] ? find(arg['id']) : find(:first, arg)
74
+ end
75
+
76
+ # -> http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-inheritance_column
77
+ def inheritance_column
78
+ (@inheritance_column ||= nil) || 'type'
79
+ end
80
+
81
+ # -> http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-quoted_table_name
82
+ def quoted_table_name
83
+ @quoted_table_name ||= "\"#{to_s.tableize}\""
84
+ end
85
+
86
+ def page(page)
87
+ @page = page.to_i
88
+ @page = 1 if @page < 1
89
+ self
90
+ end
91
+
92
+ def per(page_count)
93
+ @per_page = page_count.to_i
94
+ results
95
+ end
96
+
97
+ def ransack(params = {}, _options = {})
98
+ @ransack_params = params.blank? ? {} : params.permit!.to_h
99
+ OpenStruct.new(conditions: {}, object: OpenStruct.new(klass: self), result: self)
100
+ end
101
+
102
+ # -> http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflect_on_all_associations
103
+ def reflect_on_all_associations(_macro = nil)
104
+ []
105
+ end
106
+
107
+ # -> http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-reorder
108
+ def reorder(sql)
109
+ @order = sql
110
+ self
111
+ end
112
+
113
+ def results
114
+ results = find_all params: { page: @page, per_page: @per_page, order: @order, q: @ransack_params }
115
+ decorate_with_pagination_data(results)
116
+ end
117
+
118
+ def decorate_with_pagination_data(results)
119
+ results.current_page = @page
120
+ results.limit_value = (@connection_response['pagination-limit'] || @per_page).to_i
121
+ results.total_count = (@connection_response['pagination-totalcount'] || results.count).to_i
122
+ results.total_pages = results.limit_value > 0 ? (results.total_count.to_f / results.limit_value).ceil : 1
123
+ results
124
+ end
125
+ end
126
+
127
+ def self.prepended(base)
128
+ base.collection_parser = ActiveAdmin::ActiveResource::Results
129
+
130
+ class << base
131
+ prepend ClassMethods
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAdmin
4
+ module ActiveResource
5
+ module Connection
6
+ # ref: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Quoting.html#method-i-quote_column_name
7
+ def quote_column_name(column_name)
8
+ column_name.to_s
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_admin'
4
+ require_relative 'base'
5
+ require_relative 'connection'
4
6
 
5
7
  module ActiveAdmin
6
8
  module ActiveResource
@@ -11,133 +13,9 @@ module ActiveAdmin
11
13
  end
12
14
 
13
15
  ::ActiveResource::Base.class_eval do
14
- attr_writer :inheritance_column
15
-
16
- self.collection_parser = ActiveAdmin::ActiveResource::Results
17
-
18
- # ref: https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/ClassMethods.html#method-i-column_for_attribute
19
- def column_for_attribute(name)
20
- # => ActiveRecord::ConnectionAdapters::Column
21
- col_name = name.to_s
22
- send(:class).columns.find { |col| col.name == col_name }
23
- end
24
-
25
- class << self
26
- prepend(FindExt = Module.new do
27
- def find(*arguments)
28
- # First argument an array -> batch action
29
- if arguments.count > 0 && arguments[0].is_a?(Array)
30
- ret = []
31
- arguments[0].each do |id|
32
- ret << find(id)
33
- end
34
- ret.compact
35
- else
36
- super
37
- end
38
- end
39
- end)
40
-
41
- def _ransackers
42
- {}
43
- end
44
-
45
- # ref: https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-content_columns
46
- def content_columns
47
- @content_columns ||= columns.reject do |c|
48
- # c.name == primary_key || # required to update enities
49
- c.name == inheritance_column ||
50
- c.name.end_with?("_id") ||
51
- c.name.end_with?("_count")
52
- end
53
- end
54
-
55
- # ref: http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-column_names
56
- def column_names
57
- @column_names ||= columns.map(&:name)
58
- end
59
-
60
- # ref: http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-columns
61
- def columns
62
- # => array of ActiveRecord::ConnectionAdapters::Column
63
- @columns ||= begin
64
- schema.map do |name, type|
65
- col_name = name.to_s
66
- col_type = type.to_sym
67
- col_type = :hidden if col_name == 'id'
68
- OpenStruct.new(name: col_name, type: col_type)
69
- end
70
- end
71
- end
72
-
73
- def find_all(options = {})
74
- prefix_options, query_options = split_options(options[:params])
75
- query_options[:limit] = query_options[:per_page]
76
- path = collection_path(prefix_options, query_options)
77
- @connection_response = connection.get(path, headers)
78
- instantiate_collection((format.decode( @connection_response.body ) || []), query_options, prefix_options)
79
- end
80
-
81
- # -> http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
82
- def find_by(arg, *_args)
83
- arg && arg['id'] ? find(arg['id']) : find(:first, arg)
84
- end
85
-
86
- # -> http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-inheritance_column
87
- def inheritance_column
88
- (@inheritance_column ||= nil) || 'type'
89
- end
90
-
91
- # -> http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-quoted_table_name
92
- def quoted_table_name
93
- @quoted_table_name ||= "\"#{to_s.tableize}\""
94
- end
95
-
96
- def page(page)
97
- @page = page.to_i
98
- @page = 1 if @page < 1
99
- self
100
- end
101
-
102
- def per(page_count)
103
- @per_page = page_count.to_i
104
- results
105
- end
106
-
107
- def ransack(params = {}, _options = {})
108
- @ransack_params = params.blank? ? {} : params.permit!.to_h
109
- OpenStruct.new(conditions: {}, object: OpenStruct.new(klass: self), result: self)
110
- end
111
-
112
- # -> http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflect_on_all_associations
113
- def reflect_on_all_associations(_macro = nil)
114
- []
115
- end
116
-
117
- # -> http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-reorder
118
- def reorder(sql)
119
- @order = sql
120
- self
121
- end
122
-
123
- def results
124
- results = find_all params: { page: @page, per_page: @per_page, order: @order, q: @ransack_params }
125
- decorate_with_pagination_data(results)
126
- end
127
-
128
- def decorate_with_pagination_data(results)
129
- results.current_page = @page
130
- results.limit_value = (@connection_response['pagination-limit'] || @per_page).to_i
131
- results.total_count = (@connection_response['pagination-totalcount'] || results.count).to_i
132
- results.total_pages = results.limit_value > 0 ? (results.total_count.to_f / results.limit_value).ceil : 1
133
- results
134
- end
135
- end
16
+ prepend ActiveAdmin::ActiveResource::Base
136
17
  end
137
18
 
138
19
  ::ActiveResource::Connection.class_eval do
139
- # ref: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Quoting.html#method-i-quote_column_name
140
- def quote_column_name(column_name)
141
- column_name.to_s
142
- end
20
+ prepend ActiveAdmin::ActiveResource::Connection
143
21
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveAdmin
4
4
  module ActiveResource
5
- VERSION = '0.2.4'
5
+ VERSION = '0.2.6'
6
6
  end
7
7
  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.2.4
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Roccoberton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-13 00:00:00.000000000 Z
11
+ date: 2020-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeadmin
@@ -202,6 +202,8 @@ files:
202
202
  - README.md
203
203
  - Rakefile
204
204
  - lib/activeadmin/active_resource.rb
205
+ - lib/activeadmin/active_resource/base.rb
206
+ - lib/activeadmin/active_resource/connection.rb
205
207
  - lib/activeadmin/active_resource/engine.rb
206
208
  - lib/activeadmin/active_resource/results.rb
207
209
  - lib/activeadmin/active_resource/version.rb