Class: Blade::BladeSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/blade/setting.rb

Class Method Summary collapse

Class Method Details

.generate_base_filesObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/blade/setting.rb', line 156

def generate_base_files

  generator = Blade::Setting::ControllerTemplate::InstallGenerator.new

  application_helper = Blade::Setting::BaseTemplate::APPLICATION_HELPER
  application_builder = Blade::Setting::BaseTemplate::APPLICATION_JSON_BUILDER
  base_model_concern = Blade::Setting::BaseTemplate::BASE_MODEL_CONCERN
  response = Blade::Setting::BaseTemplate::RESPONSE
  response_json = Blade::Setting::BaseTemplate::RESPONSE_JSON

  application_helper_path = Rails.root.join('app', 'helpers', 'application_helper.rb')
  application_builder_path = Rails.root.join('app', 'views', 'layouts', 'application.json.jbuilder')
  response_path = Rails.root.join('app', 'models', 'response.rb')
  base_model_concern_path = Rails.root.join('app', 'models', 'concerns', 'base_model_concern.rb')
  response_json_path = Rails.root.join('app', 'views', 'common', '_response_status.json.jbuilder')

  generator.create_view_file(application_builder_path, application_builder)
  generator.create_view_file(application_helper_path, application_helper)
  generator.create_view_file(response_path, response)
  generator.create_view_file(base_model_concern_path, base_model_concern)
  generator.create_view_file(response_json_path, response_json)
end

.generate_crud(model_class, name, namespace) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/blade/setting.rb', line 86

def generate_crud(model_class, name, namespace)

  model = model_class.name
  arg1 = name
  args = arg1.pluralize

  #generate routes
  Blade::Setting::ControllerTemplate::InstallGenerator.new.add_routes(args, namespace)

  #generate controller
  con_path = Rails.root.join('app', 'controllers', namespace, "#{args}_controller.rb")
  con_file = Blade::Setting::ControllerTemplate::InstallGenerator.controller_tmp(model_class, name, namespace);
  Blade::Setting::ControllerTemplate::InstallGenerator.new.create_view_file(con_path, con_file)


  #generate  views
  index_view_path = Rails.root.join('app', 'views', namespace, args.to_s, 'index.json.jbuilder')
  common_view_path = Rails.root.join('app', 'views', namespace, 'common', "_#{arg1}.json.jbuilder")
  update_view_path = Rails.root.join('app', 'views', namespace, args.to_s, 'update.json.jbuilder')
  create_view_path = Rails.root.join('app', 'views', namespace, args.to_s, 'create.json.jbuilder')
  delete_view_path = Rails.root.join('app', 'views', namespace, args.to_s, 'destroy.json.jbuilder')

  index_content = <<-File

json.#{args} do
if @#{args}.present?
  render_json_array_partial(json,@#{args},'#{namespace}/common/#{arg1}',:#{arg1})
else
  {}
end
end
  File
  common_view_content = <<-File
if #{arg1}.present?
  render_json_attrs(json, #{arg1})
 else
  json.#{arg1} {}
end

  File

  create_view_content = <<-File
if @#{arg1}.present?
  json.#{arg1} do
    render_json_attrs(json, @#{arg1})
  end
 else
   json.#{arg1} {}
end
  File
  #
  update_view_content = <<-File
json.#{arg1} do
  if @#{arg1}.present?
    render_json_attrs(json,@#{arg1})
  else
    {}
  end
end

  File

  Blade::Setting::ControllerTemplate::InstallGenerator.new.create_view_file(index_view_path, index_content)
  Blade::Setting::ControllerTemplate::InstallGenerator.new.create_view_file(create_view_path, create_view_content)
  Blade::Setting::ControllerTemplate::InstallGenerator.new.create_view_file(update_view_path, update_view_content)
  Blade::Setting::ControllerTemplate::InstallGenerator.new.create_view_file(common_view_path, common_view_content)
  Blade::Setting::ControllerTemplate::InstallGenerator.new.create_view_file(delete_view_path, '')

end

.generate_custom_yml(name) ⇒ Object



179
180
181
# File 'lib/blade/setting.rb', line 179

def generate_custom_yml(name)
  gt = Blade::Setting::ControllerTemplate::InstallGenerator.new
end

.generate_doc(model, name) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/blade/setting.rb', line 192

def generate_doc(model, name)
  file = <<File
require 'swagger_helper'

describe '#{name}模块' do

#{Blade::Setting::SwaggerTemplate.generate_template(model, name)}

end

File

  path = Rails.root.join('spec', 'integration', "#{model}_spec.rb")
  gt.create_view_file(path, file)
end

.generate_gem(name) ⇒ Object



188
189
190
# File 'lib/blade/setting.rb', line 188

def generate_gem(name)
  gt.add_gem(name)
end

.generate_model_template(model_class, name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/blade/setting.rb', line 56

def generate_model_template(model_class, name)
  model_file = Blade::Setting::ModelTemplate.model_template(model_class)
  path = Dir.pwd + "/app/models/" + name + ".rb"
  gt = Blade::Setting::ControllerTemplate::InstallGenerator.new
  file = nil
  file_path = Rails.root.join('app', 'models', "#{name}.rb")
  # if File.exist?(path)
  #   file = File.open(path, "w")
  # else
  #   file = File.new(path, "w")
  # end
  # file.write model_file;
  gt.create_view_file(file_path, model_file)
end

.generate_service_template(model_class, name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/blade/setting.rb', line 71

def generate_service_template(model_class,name)
  model_file = Blade::Setting::ServiceTemplate.service_template(model_class)
  path = Dir.pwd + "/app/services/" + name + "_service.rb"
  gt = Blade::Setting::ControllerTemplate::InstallGenerator.new
  file = nil
  file_path = Rails.root.join('app', 'services', "#{name}_service.rb")
  # if File.exist?(path)
  #   file = File.open(path, "w")
  # else
  #   file = File.new(path, "w")
  # end
  # file.write model_file;
  gt.create_view_file(file_path, model_file)
end

.generate_setting_logic(setting_path, configs = []) ⇒ Object

if in_rails_application? || in_rails_application_subdirectory?

exit(0)

else

exit(1)

end



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/blade/setting.rb', line 21

def generate_setting_logic(setting_path, configs = [])

  configs.each do |config|

    path = Dir.pwd
    unless Dir.exist?(path + setting_path)
      FileUtils.mkdir_p path + setting_path
    end

    file = File.new(path + setting_path + '/' + config + '_setting.rb', 'w')
    file.write <<-File
# frozen_string_literal: true
class #{config.camelize}Setting < Settingslogic
  source "#{'#{Rails.root}'}/config/#{config}.yml"
  namespace Rails.env
end
    File
  end
end

.generate_setting_yml(yml_base_path, extra_path, configs = []) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/blade/setting.rb', line 41

def generate_setting_yml(yml_base_path, extra_path, configs = [])
  all_environment_paths = extra_path
  all_environment_paths.each do |env_path|
    path = Dir.pwd
    unless Dir.exist?(path + yml_base_path + '/' + env_path)
      FileUtils.mkdir_p path + yml_base_path + '/' + env_path + '/' + 'config'
    end
    configs.each do |config|
      yml_file = "Blade::Setting::YmlTemplate::#{config.upcase}_YML".constantize
      file = File.new(path + yml_base_path + '/' + env_path + '/' + 'config' '/' + config + '.yml', 'w')
      file.write yml_file
    end
  end
end

.get_model(name) ⇒ Object



184
185
186
# File 'lib/blade/setting.rb', line 184

def get_model(name)
  ActiveSupport::Dependencies.constantize(name.classify)
end

.gtObject



208
209
210
# File 'lib/blade/setting.rb', line 208

def gt
  Blade::Setting::ControllerTemplate::InstallGenerator.new
end