Module: ActiveAdmin::ActiveResource::Base::ClassMethods

Defined in:
lib/activeadmin/active_resource/base.rb

Instance Method Summary collapse

Instance Method Details

#_ransackersObject



31
32
33
# File 'lib/activeadmin/active_resource/base.rb', line 31

def _ransackers
  {}
end

#column_namesObject



46
47
48
# File 'lib/activeadmin/active_resource/base.rb', line 46

def column_names
  @column_names ||= columns.map(&:name)
end

#columnsObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/activeadmin/active_resource/base.rb', line 51

def columns
  # => array of ActiveRecord::ConnectionAdapters::Column
  @columns ||=
    schema.map do |name, type|
      col_name = name.to_s
      col_type = type.to_sym
      col_type = :hidden if col_name == 'id'
      OpenStruct.new(name: col_name, type: col_type)
    end
end

#content_columnsObject



36
37
38
39
40
41
42
43
# File 'lib/activeadmin/active_resource/base.rb', line 36

def content_columns
  @content_columns ||= columns.reject do |c|
    # c.name == primary_key || # required to update enities
    c.name == inheritance_column ||
      c.name.end_with?("_id") ||
      c.name.end_with?("_count")
  end
end

#decorate_with_pagination_data(results) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/activeadmin/active_resource/base.rb', line 117

def decorate_with_pagination_data(results)
  results.current_page = @page
  results.limit_value = (@connection_response['pagination-limit'] || @per_page).to_i
  results.total_count = (@connection_response['pagination-totalcount'] || results.count).to_i
  results.total_pages = results.limit_value > 0 ? (results.total_count.to_f / results.limit_value).ceil : 1
  results
end

#find_all(options = {}) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/activeadmin/active_resource/base.rb', line 62

def find_all(options = {})
  prefix_options, query_options = split_options(options[:params])
  query_options[:limit] = query_options[:per_page]
  path = collection_path(prefix_options, query_options)
  @connection_response = connection.get(path, headers)
  instantiate_collection((format.decode( @connection_response.body ) || []), query_options, prefix_options)
end

#find_by(arg, *_args) ⇒ Object



71
72
73
# File 'lib/activeadmin/active_resource/base.rb', line 71

def find_by(arg, *_args)
  arg && arg['id'] ? find(arg['id']) : find(:first, arg)
end

#inheritance_columnObject



76
77
78
# File 'lib/activeadmin/active_resource/base.rb', line 76

def inheritance_column
  (@inheritance_column ||= nil) || 'type'
end

#page(page) ⇒ Object



85
86
87
88
89
# File 'lib/activeadmin/active_resource/base.rb', line 85

def page(page)
  @page = page.to_i
  @page = 1 if @page < 1
  self
end

#per(page_count) ⇒ Object



91
92
93
94
# File 'lib/activeadmin/active_resource/base.rb', line 91

def per(page_count)
  @per_page = page_count.to_i
  results
end

#quoted_table_nameObject



81
82
83
# File 'lib/activeadmin/active_resource/base.rb', line 81

def quoted_table_name
  @quoted_table_name ||= "\"#{to_s.tableize}\""
end

#ransack(params = {}, _options = {}) ⇒ Object



96
97
98
99
# File 'lib/activeadmin/active_resource/base.rb', line 96

def ransack(params = {}, _options = {})
  @ransack_params = params.blank? ? {} : params.permit!.to_h
  OpenStruct.new(conditions: {}, object: OpenStruct.new(klass: self), result: self)
end

#reflect_on_all_associations(_macro = nil) ⇒ Object



102
103
104
# File 'lib/activeadmin/active_resource/base.rb', line 102

def reflect_on_all_associations(_macro = nil)
  []
end

#reorder(sql) ⇒ Object



107
108
109
110
# File 'lib/activeadmin/active_resource/base.rb', line 107

def reorder(sql)
  @order = sql
  self
end

#resultsObject



112
113
114
115
# File 'lib/activeadmin/active_resource/base.rb', line 112

def results
  results = find_all params: { page: @page, per_page: @per_page, order: @order, q: @ransack_params }
  decorate_with_pagination_data(results)
end