/
resource_generator.rb
64 lines (49 loc) · 1.48 KB
/
resource_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
module ActiveAdmin
module Generators
class ResourceGenerator < Rails::Generators::NamedBase
desc "Registers resources with Active Admin"
source_root File.expand_path("templates", __dir__)
def generate_config_file
template "resource.rb.erb", "app/admin/#{file_path.tr('/', '_').pluralize}.rb"
end
protected
def attributes
@attributes ||= class_name.constantize.new.attributes.keys
end
def primary_key
@primary_key ||= [class_name.constantize.primary_key].flatten
end
def assignable_attributes
@assignable_attributes ||= attributes - primary_key - %w(created_at updated_at)
end
def permit_params
assignable_attributes.map { |a| a.to_sym.inspect }.join(", ")
end
def rows
attributes.map { |a| row(a) }.join("\n ")
end
def row(name)
"row :#{name.gsub(/_id$/, '')}"
end
def columns
(attributes - primary_key).map { |a| column(a) }.join("\n ")
end
def column(name)
"column :#{name.gsub(/_id$/, '')}"
end
def filters
attributes.map { |a| filter(a) }.join("\n ")
end
def filter(name)
"filter :#{name.gsub(/_id$/, '')}"
end
def form_inputs
assignable_attributes.map { |a| form_input(a) }.join("\n ")
end
def form_input(name)
"f.input :#{name.gsub(/_id$/, '')}"
end
end
end
end