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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/blade/setting/service_template.rb', line 10
def service_template(model_class)
model = model_class.name
arg1 = model_class.name.downcase
args = arg1.pluralize
model_columns = model_class.columns.map(&:name)
model_columns_key = model_columns.map(&:to_sym)
belongs_association = []
model_columns.each do |column|
if (column.include?("_id"))
belongs_model = column.split("_id")[0]
begin
if ActiveSupport::Dependencies.constantize(belongs_model.classify).present?
belongs_association.push "belongs_to :#{belongs_model}"
end
rescue => e
next
end
end
end
model_file = <<-file
class #{model}Service
class << self
def create_by_params(params)
#{arg1} = nil
response = Response.rescue do |res|
user = params[:user]
create_params = params.require(:create).permit!
# 验证必填参数
must_params = params.require(:create).values
res.raise_error("缺少参数") if #{model}.validate_blank?(must_params)
#{arg1} = #{model}.new(create_params)
#{arg1}.save!
end
return response, #{arg1}
end
def update_by_params(params)
#{arg1}= nil
response = Response.rescue do |res|
user = params[:user]
#{arg1}_id = params[:#{arg1}_id]
res.raise_error("缺少参数") if #{arg1}_id.blank?
update_params = params.require(:update).permit!
#{arg1} = #{model}.find(#{arg1}_id)
res.raise_data_miss_error("#{arg1}不存在") if #{arg1}.blank?
#{arg1}.update_attributes!(update_params)
end
return response, #{arg1}
end
def query_by_params(params)
#{args} = nil
response = Response.rescue do |res|
page, per, search_param = params[:page] || 1, params[:per], params[:search]
search_param = {} if search_param.blank?
#{args} = #{model}.search_by_params(search_param)
if per.present?
#{args} = #{args}.page(page).per(per)
end
end
return response, #{args}
end
def delete_by_params(params)
#{arg1} = nil
response = Response.rescue do |res|
#{arg1}_id = params[:#{arg1}_id]
res.raise_error("参数缺失") if #{arg1}_id.blank?
#{arg1} = #{model}.find(#{arg1}_id)
res.raise_data_miss_error("#{model}不存在") if #{arg1}.blank?
#{arg1}.destroy!
end
return response
end
end
end
file
return model_file
end
|