Skip to content

Commit

Permalink
Add ingredient generator
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Jun 28, 2021
1 parent c5ea77a commit c71efbe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/generators/alchemy/ingredient/ingredient_generator.rb
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 lib/generators/alchemy/ingredient/templates/editor.html.erb
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 %>
14 changes: 14 additions & 0 deletions lib/generators/alchemy/ingredient/templates/model.rb.tt
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
1 change: 1 addition & 0 deletions lib/generators/alchemy/ingredient/templates/view.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%%= <%= @ingredient_view_local %>.value %>

0 comments on commit c71efbe

Please sign in to comment.