require_relative '../blade/setting/yml_template' require_relative '../blade/setting/model_template' require_relative '../blade/setting/controller_template' require_relative '../blade/setting/base_template' require 'rails/generators' module Blade class BladeSetting class << self # if in_rails_application? || in_rails_application_subdirectory? # exit(0) # else # exit(1) # end # 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 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 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 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 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 def generate_custom_yml(name) gt = Blade::Setting::ControllerTemplate::InstallGenerator.new end def get_model(name) ActiveSupport::Dependencies.constantize(name.classify) end def generate_gem(name) gt.add_gem(name) end def gt Blade::Setting::ControllerTemplate::InstallGenerator.new end end end end