lib/puffer/controller/dsl.rb in puffer-0.0.12 vs lib/puffer/controller/dsl.rb in puffer-0.0.13

- old
+ new

@@ -3,19 +3,11 @@ module Dsl def self.included base base.class_eval do extend ClassMethods - include ActionMethods - extend ActionMethods - %w(index show form create update).each do |action| - class_attribute "_#{action}_fields" - send "_#{action}_fields=", Puffer::Fields.new unless send("_#{action}_fields?") - helper_method "#{action}_fields" - end - class_attribute :_members self._members = Puffer::Controller::Actions.new class_attribute :_collections self._collections = Puffer::Controller::Actions.new end @@ -35,45 +27,37 @@ def collection &block block.bind(_collections).call if block_given? end - %w(index show form create update).each do |action| - define_method action do |&block| - @_fields = send("_#{action}_fields=", Puffer::Fields.new) - block.call if block - remove_instance_variable :@_fields + def define_fields *actions + actions.each do |action| + class_attribute "_#{action}_fields" + send "_#{action}_fields=", Puffer::Fields.new unless send("_#{action}_fields?") + helper_method "#{action}_fields" + + self.class.instance_eval do + define_method action do |&block| + @_fields = send("_#{action}_fields=", Puffer::Fields.new) + block.call if block + remove_instance_variable :@_fields + end + + define_method "#{action}_fields" do + send "_#{action}_fields" + end + end + + define_method "#{action}_fields" do + self.class.send "#{action}_fields" + end end end def field name, options = {} field = @_fields.field(model, name, options) if @_fields generate_association_actions field if field.reflection #generate_change_actions field if field.toggable? - end - - end - - module ActionMethods - - def index_fields - _index_fields - end - - def show_fields - _show_fields.presence || _index_fields - end - - def form_fields - _form_fields - end - - def create_fields - _create_fields.presence || _form_fields - end - - def update_fields - _update_fields.presence || _form_fields end end end