Skip to content

Commit

Permalink
Merge pull request #80 from eudoxa/fix_generator
Browse files Browse the repository at this point in the history
fix generator code
  • Loading branch information
eudoxa authored May 9, 2024
2 parents ba10ef7 + 1baba2c commit 225e075
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/generators/chanko/unit/templates/unit.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module <%= class_name %>
# ```

# ## raise_error
# `raise_error` is used to force an unit to raise up errors occured in invoking.
# `raise_error` is used to force an unit to raise up errors occurred in invoking.
# You can force to raise up errors also by `Config.raise_error`.
#
# ```
Expand All @@ -38,7 +38,7 @@ module <%= class_name %>
# ```
# scope(:view) do
# function(:function_name) do
# render "/units/<%= your_unit_name.underscore %>/example", :foo => "bar"
# render "/units/<%= file_name.underscore %>/example", :foo => "bar"
# end
# end
# ```
Expand Down
35 changes: 22 additions & 13 deletions lib/generators/chanko/unit/unit_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,53 @@ class UnitGenerator < Rails::Generators::NamedBase
source_root File.expand_path("../templates", __FILE__)

def create_unit_directory
empty_directory(directory)
empty_directory(unit_directory)
end

def create_unit_file
template("unit.rb.erb", "#{directory}/#{file_name}.rb")
template("unit.rb.erb", "#{unit_directory}/#{file_name}.rb")
end

def create_views_directory
create_file("#{directory}/views/.gitkeep", "")
def create_views_unit_directory
create_file("#{unit_directory}/views/.gitkeep", "")
end

ASSETS_TYPES.each do |type|
define_method("create_#{type}_directory") do
create_file("#{directory}/#{type}/.gitkeep", "")
define_method("create_#{type}_unit_directory") do
create_file("#{unit_directory}/#{type}/.gitkeep", "")
end
end

ASSETS_TYPES.each do |type|
define_method("create_#{type}_symlink") do
define_method("create_#{type}_unit_symlink") do
create_assets_symlink(type)
end
end

def create_views_symlink
from = "app/views/#{units_directory_name}/#{file_name}"
to = "../../../#{relative_unit_directory}/views"
create_link(from, to)
end

private

def create_assets_symlink(type)
from = "app/assets/#{type}/#{directory_name}/#{file_name}"
to = "../../../../#{directory}/#{type}"
from = "app/assets/#{type}/#{units_directory_name}/#{file_name}"
to = "../../../../#{relative_unit_directory}/#{type}"
create_link(from, to)
end

def directory
def relative_unit_directory
Pathname.new(unit_directory).relative_path_from(Rails.root)
end

def unit_directory
"#{Chanko::Config.units_directory_path}/#{file_name}"
end

def directory_name
def units_directory_name
Chanko::Config.units_directory_path.split("/").last
end
end
end
end

0 comments on commit 225e075

Please sign in to comment.