[Groonga-commit] ranguba/groonga-client-model at d65ea5f [master] Support generating migration file by model generation

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Mar 6 16:46:48 JST 2017


Kouhei Sutou	2017-03-06 16:46:48 +0900 (Mon, 06 Mar 2017)

  New Revision: d65ea5f4eb3c5306e6b22130107957e2d8d11b3f
  https://github.com/ranguba/groonga-client-model/commit/d65ea5f4eb3c5306e6b22130107957e2d8d11b3f

  Message:
    Support generating migration file by model generation

  Modified files:
    lib/groonga_client_model/railtie.rb
    lib/rails/generators/groonga_client_model/migration/templates/migration.rb
    lib/rails/generators/groonga_client_model/migration_generator.rb
    lib/rails/generators/groonga_client_model/model_generator.rb

  Modified: lib/groonga_client_model/railtie.rb (+3 -1)
===================================================================
--- lib/groonga_client_model/railtie.rb    2017-03-06 13:26:12 +0900 (6c28f1a)
+++ lib/groonga_client_model/railtie.rb    2017-03-06 16:46:48 +0900 (b4790a5)
@@ -22,7 +22,9 @@ module GroongaClientModel
     config.groonga_client_model = ActiveSupport::OrderedOptions.new
 
     unless config.app_generators.rails[:orm]
-      config.app_generators.orm(:groonga_client_model)
+      config.app_generators.orm(:groonga_client_model,
+                                migration: true,
+                                timestamps: true)
     end
 
     config.action_dispatch.rescue_responses.merge!(

  Modified: lib/rails/generators/groonga_client_model/migration/templates/migration.rb (+3 -2)
===================================================================
--- lib/rails/generators/groonga_client_model/migration/templates/migration.rb    2017-03-06 13:26:12 +0900 (31ef293)
+++ lib/rails/generators/groonga_client_model/migration/templates/migration.rb    2017-03-06 16:46:48 +0900 (b0727ac)
@@ -1,9 +1,10 @@
 class <%= migration_class_name %> < GroongaClientModel::Migration
   def change
 <%- attributes.each do |attribute| -%>
-  <%- if migration_action == "add" -%>
+  <%- case @migration_action -%>
+  <%- when "add" -%>
     add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
-  <%- else -%>
+  <%- when "remove" -%>
     remove_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
   <%- end -%>
 <%- end -%>

  Modified: lib/rails/generators/groonga_client_model/migration_generator.rb (+4 -3)
===================================================================
--- lib/rails/generators/groonga_client_model/migration_generator.rb    2017-03-06 13:26:12 +0900 (3acd94f)
+++ lib/rails/generators/groonga_client_model/migration_generator.rb    2017-03-06 16:46:48 +0900 (f1af24f)
@@ -52,16 +52,17 @@ module GroongaClientModel
 
       def create_migration_file
         IllegalMigrationNameError.validate(file_name)
-        decide_template
+        decide_template(file_name)
         migration_template(@migration_template,
                            File.join("db/groonga/migrate", "#{file_name}.rb"))
       end
 
       private
-      def decide_template
+      def decide_template(output_file_name)
         @migration_template = "migration.rb"
+        @migration_action = nil
         @key_type = nil
-        case file_name
+        case output_file_name
         when /\A(add|remove)_.*_(?:to|from)_(.*)\z/
           @migration_action = $1
           @table_name = normalize_table_name($2)

  Modified: lib/rails/generators/groonga_client_model/model_generator.rb (+13 -0)
===================================================================
--- lib/rails/generators/groonga_client_model/model_generator.rb    2017-03-06 13:26:12 +0900 (2811137)
+++ lib/rails/generators/groonga_client_model/model_generator.rb    2017-03-06 16:46:48 +0900 (e9a78c2)
@@ -36,6 +36,19 @@ module GroongaClientModel
                    desc: "The parent class for the generated model"
 
       hook_for :test_framework
+      hook_for :migration do |klass|
+        args = ["create_#{table_name}"]
+        attributes.each do |attribute|
+          arg = "#{attribute.name}:#{attribute.type}"
+          if attribute.has_uniq_index?
+            arg << ":unique"
+          elsif attribute.has_index?
+            arg << ":index"
+          end
+          args << arg
+        end
+        invoke(klass, args)
+      end
 
       def create_model_file
         generate_application_groonga_record
-------------- next part --------------
HTML����������������������������...
다운로드 



More information about the Groonga-commit mailing list
Back to archive index