-
-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
require "rails" | ||
|
||
module Alchemy | ||
module Generators | ||
class IngredientGenerator < ::Rails::Generators::Base | ||
desc "This generator generates an Alchemy ingredient class for you." | ||
argument :class_name, banner: "ingredient_class_name" | ||
source_root File.expand_path("templates", __dir__) | ||
|
||
def init | ||
@class_name = class_name.classify | ||
@ingredients_view_path = "app/views/alchemy/ingredients" | ||
end | ||
|
||
def create_model | ||
template "model.rb.tt", "app/models/alchemy/ingredients/#{file_name}.rb" | ||
end | ||
|
||
def copy_templates | ||
@ingredient_editor_local = "#{file_name}_editor" | ||
@ingredient_view_local = "#{file_name}_view" | ||
template "view.html.erb", "#{@ingredients_view_path}/_#{file_name}_view.html.erb" | ||
template "editor.html.erb", "#{@ingredients_view_path}/_#{file_name}_editor.html.erb" | ||
end | ||
|
||
def show_todo | ||
say "\nPlease check the generated files and alter them to fit your needs." | ||
end | ||
|
||
private | ||
|
||
def file_name | ||
@_file_name ||= @class_name.classify.demodulize.underscore | ||
end | ||
end | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
lib/generators/alchemy/ingredient/templates/editor.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<%%# | ||
Available locals: | ||
* <%= @ingredient_editor_local %> - An Alchemy::IngredientEditor instance | ||
|
||
Please consult Alchemy::IngredientEditor.rb docs for further methods on the ingredient object | ||
%> | ||
<%%= content_tag :div, | ||
class: <%= @ingredient_editor_local %>.css_classes, | ||
data: <%= @ingredient_editor_local %>.data_attributes do %> | ||
<%%= element_form.fields_for(:ingredients, <%= @ingredient_editor_local %>.ingredient) do |f| %> | ||
<%%= ingredient_label(<%= @ingredient_editor_local %>) %> | ||
<%%= f.text_field :value %> | ||
<%% end %> | ||
<%% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
module Ingredients | ||
class <%= @class_name %> < Alchemy::Ingredient | ||
# Set additional attributes that get stored in the data JSON column | ||
# Alchemy will create attribute accessors for each of it. | ||
# self.ingredient_attributes = %i[] | ||
|
||
# Set a related_object alias for convenience | ||
# related_object_alias :some_association_name, class_name: "Some::Klass" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%%= <%= @ingredient_view_local %>.value %> |