Skip to content

Commit

Permalink
clean view generator
Browse files Browse the repository at this point in the history
  • Loading branch information
gagalago committed Jan 27, 2021
1 parent 5f71ed8 commit 9d16d76
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class <%= migration_class_name %> < <%= activerecord_migration_class %>
def change
<%- if materialized? -%>
update_view <%= formatted_plural_name %>,
version: <%= version %>,
revert_to_version: <%= previous_version %>,
version: <%= definition.version %>,
revert_to_version: <%= previous_definition.version %>,
materialized: <%= no_data? ? "{ no_data: true }" : true %>
<%- else -%>
update_view <%= formatted_plural_name %>, version: <%= version %>, revert_to_version: <%= previous_version %>
update_view <%= formatted_plural_name %>, version: <%= definition.version %>, revert_to_version: <%= previous_definition.version %>
<%- end -%>
end
end
18 changes: 6 additions & 12 deletions lib/generators/scenic/view/view_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def create_migration_file
if creating_new_view? || destroying_initial_view?
migration_template(
"db/migrate/create_view.erb",
"db/migrate/create_#{plural_file_name}.rb",
"db/migrate/#{migration_class_name.underscore}.rb",
)
else
migration_template(
"db/migrate/update_view.erb",
"db/migrate/update_#{plural_file_name}_to_version_#{version}.rb",
"db/migrate/#{migration_class_name.underscore}.rb",
)
end
end
Expand All @@ -45,19 +45,11 @@ def self.next_migration_number(dir)
end

no_tasks do
def previous_version
@previous_version ||= previous_definition.version
end

def version
@version ||= destroying? ? previous_version : previous_version.next
end

def migration_class_name
if creating_new_view?
"Create#{class_name.tr('.', '').pluralize}"
else
"Update#{class_name.pluralize}ToVersion#{version}"
"Update#{class_name.pluralize}ToVersion#{definition.version}"
end
end

Expand Down Expand Up @@ -87,6 +79,8 @@ def creating_new_view?
end

def definition
previous_version = previous_definition.version
version = destroying? ? previous_version : previous_version.next
Scenic::Definition.new(plural_file_name, version)
end

Expand Down Expand Up @@ -115,7 +109,7 @@ def create_view_options
end

def destroying_initial_view?
destroying? && version == 1
destroying? && definition.version == 1
end
end
end
Expand Down

0 comments on commit 9d16d76

Please sign in to comment.