class FormGenerator
Public Instance Methods
generate_form()
click to toggle source
# File lib/generators/form/form_generator.rb, line 40 def generate_form template 'form.rb', File.join('app/lib/forms', transformed_class_path, "#{file_name}.rb") end
generate_form_spec()
click to toggle source
# File lib/generators/form/form_generator.rb, line 44 def generate_form_spec template 'form_spec.rb', File.join('spec/lib/forms', transformed_class_path, "#{file_name}_spec.rb") end
transform_naming()
click to toggle source
# File lib/generators/form/form_generator.rb, line 25 def transform_naming return unless pluralize_collection? self.name = begin names = name.split('/') names[0..-3].append(names[-2].pluralize).append(names[-1]).join('/') end end
validate_fixed_attrs()
click to toggle source
# File lib/generators/form/form_generator.rb, line 34 def validate_fixed_attrs return unless duplicate_attributes.any? raise "Cannot have duplicate attributes: #{duplicate_attributes.join(', ')}" end
Private Instance Methods
all_attributes()
click to toggle source
# File lib/generators/form/form_generator.rb, line 50 def all_attributes @all_attributes ||= begin flattened_form_attributes .map(&:strip) .sort end end
all_attributes_to_pass()
click to toggle source
# File lib/generators/form/form_generator.rb, line 58 def all_attributes_to_pass @all_attributes_to_pass ||= begin ret = flattened_form_attributes .map(&:strip) .sort ret.delete(first_attribute) if create? ret end end
attributes_and_delegates()
click to toggle source
# File lib/generators/form/form_generator.rb, line 72 def attributes_and_delegates @attributes_and_delegates ||= Hash[ form_attributes .map { |e| e.split(/:|,/).map(&method(:variablefy)) } .collect { |v| [v[0], v[1..-1]] } ] end
attributes_with_delegates()
click to toggle source
# File lib/generators/form/form_generator.rb, line 68 def attributes_with_delegates @attributes_with_delegates ||= attributes_and_delegates.keys.sort end
collection()
click to toggle source
# File lib/generators/form/form_generator.rb, line 80 def collection @collection ||= split_name[-2].pluralize.underscore end
collection_name()
click to toggle source
# File lib/generators/form/form_generator.rb, line 84 def collection_name @collection_name ||= collection.camelcase end
create?()
click to toggle source
# File lib/generators/form/form_generator.rb, line 88 def create? form == 'create' end
delegated_attributes()
click to toggle source
# File lib/generators/form/form_generator.rb, line 92 def delegated_attributes @delegated_attributes ||= attributes_and_delegates .select { |_k, v| v.sort!.present? } end
destroy?()
click to toggle source
# File lib/generators/form/form_generator.rb, line 107 def destroy? form == 'destroy' end
destroy_attribute()
click to toggle source
# File lib/generators/form/form_generator.rb, line 97 def destroy_attribute @destroy_attribute ||= begin if all_attributes.include?(collection.singularize) collection.singularize else first_attribute end end end
duplicate_attributes()
click to toggle source
# File lib/generators/form/form_generator.rb, line 111 def duplicate_attributes @duplicate_attributes ||= all_attributes .group_by { |e| e } .select { |_k, v| v.size > 1 } .map(&:first) end
factories()
click to toggle source
# File lib/generators/form/form_generator.rb, line 134 def factories return unless factory_bot? @factories ||= begin FactoryBot.factories.map(&:name) end end
factory?(attr)
click to toggle source
# File lib/generators/form/form_generator.rb, line 118 def factory?(attr) return false unless factory_bot? factories.include?(attr.to_sym) end
factory_bot?()
click to toggle source
# File lib/generators/form/form_generator.rb, line 124 def factory_bot? @factory_bot ||= begin require 'factory_bot' FactoryBot.find_definitions if FactoryBot.factories.count.zero? Class.const_defined?('FactoryBot') end rescue LoadError false end
first_attribute()
click to toggle source
# File lib/generators/form/form_generator.rb, line 142 def first_attribute @first_attribute ||= flattened_form_attributes.first end
flattened_form_attributes()
click to toggle source
# File lib/generators/form/form_generator.rb, line 146 def flattened_form_attributes @flattened_form_attributes ||= form_attributes .map { |e| e.split(/:|,/) } .flatten .map(&method(:variablefy)) end
form()
click to toggle source
# File lib/generators/form/form_generator.rb, line 153 def form @form ||= variablefy(name.split('/').last) end
form_name()
click to toggle source
# File lib/generators/form/form_generator.rb, line 157 def form_name @form_name ||= form.camelcase end
inferred_model_name()
click to toggle source
# File lib/generators/form/form_generator.rb, line 161 def inferred_model_name @inferred_model_name ||= collection.singularize.camelcase end
lock_key()
click to toggle source
# File lib/generators/form/form_generator.rb, line 165 def lock_key if create? [":#{collection}", ':create'] elsif update? [":#{collection}", method_attribute.to_s] elsif upsert? [":#{collection}", method_attribute.to_s] else ['LOCK_KEY'] end end
method_attribute()
click to toggle source
# File lib/generators/form/form_generator.rb, line 177 def method_attribute @method_attribute ||= begin if all_attributes.include?(collection.singularize) collection.singularize else first_attribute end end end
method_name()
click to toggle source
# File lib/generators/form/form_generator.rb, line 187 def method_name if create? "create_#{collection.singularize}" elsif update? "update_#{collection.singularize}" elsif upsert? "upsert_#{collection.singularize}" else form end end
module_namespacing(&block)
click to toggle source
# File lib/generators/form/form_generator.rb, line 199 def module_namespacing(&block) content = capture(&block) modules.reverse.each do |mod| content = wrap_with_module(content, mod) end concat(content) end
modules()
click to toggle source
# File lib/generators/form/form_generator.rb, line 207 def modules @modules ||= ['Forms'] + split_name[0..-2].map(&:to_s).map(&:camelcase) end
pluralize_collection?()
click to toggle source
# File lib/generators/form/form_generator.rb, line 211 def pluralize_collection? @pluralize_collection ||= options[:pluralize_collection] end
return_attribute()
click to toggle source
# File lib/generators/form/form_generator.rb, line 215 def return_attribute @return_attribute ||= begin if all_attributes.include?(collection.singularize) collection.singularize elsif !form_attributes.first.nil? form_attributes.first.split(':').first end end end
scope_names()
click to toggle source
# File lib/generators/form/form_generator.rb, line 229 def scope_names @scope_names ||= scopes.map(&:camelcase) end
scopes()
click to toggle source
# File lib/generators/form/form_generator.rb, line 225 def scopes @scopes ||= split_name[0..-3].map(&:underscore) end
spec_comments?()
click to toggle source
# File lib/generators/form/form_generator.rb, line 233 def spec_comments? @spec_comments ||= options[:spec_comments] end
split_name()
click to toggle source
# File lib/generators/form/form_generator.rb, line 237 def split_name @split_name ||= name.split('/') end
transformed_class_path()
click to toggle source
# File lib/generators/form/form_generator.rb, line 241 def transformed_class_path @transformed_class_path ||= begin if pluralize_collection? class_path[0..-2].append(class_path[-1].pluralize) else class_path end end end
update?()
click to toggle source
# File lib/generators/form/form_generator.rb, line 251 def update? form == 'update' end
upsert?()
click to toggle source
# File lib/generators/form/form_generator.rb, line 255 def upsert? form == 'upsert' end
upsert_delegates()
click to toggle source
# File lib/generators/form/form_generator.rb, line 259 def upsert_delegates @upsert_delegates ||= begin if delegated_attributes.key?(method_attribute) delegated_attributes[method_attribute] else [] end end end
variablefy(val)
click to toggle source
# File lib/generators/form/form_generator.rb, line 269 def variablefy(val) val.to_s.underscore end
wrap_with_module(content, mod)
click to toggle source
# File lib/generators/form/form_generator.rb, line 273 def wrap_with_module(content, mod) content = indent(content).chomp "module #{mod}\n#{content}\nend\n" end