lib/puffer/controller/dsl.rb in puffer-0.0.5 vs lib/puffer/controller/dsl.rb in puffer-0.0.6
- old
+ new
@@ -2,62 +2,60 @@
module Controller
module Dsl
def self.included base
base.class_eval do
- class_attribute :puffer_fields
- self.puffer_fields = {}
-
extend ClassMethods
+ include ActionMethods
+ extend ActionMethods
- helper_method :index_fields, :show_fields, :form_fields, :create_fields, :update_fields
+ %w(index show form create update).each do |action|
+ class_attribute "_#{action}_fields"
+ send "_#{action}_fields=", Puffer::Fields.new unless send("_#{action}_fields").present?
+ helper_method "#{action}_fields"
+ end
end
end
- [:index, :show, :form, :create, :update].each do |sym|
- define_method "#{sym}_fields" do
- self.class.send "#{sym}_fields"
- end
- end
-
module ClassMethods
- def configure &block
- block.bind(current_config).call
+ %w(index show form create update).each do |action|
+ define_method action do |&block|
+ @_fields = send "_#{action}_fields"
+ @_fields.clear
+ block.call if block
+ @_fields = nil
+ end
end
- [:index, :show, :form, :create, :update].each do |sym|
- define_method sym do
- @puffer_option = sym
- yield if block_given?
- end
+ def field name, options = {}
+ field = @_fields.field(model, name, options) if @_fields
+ #generate_association_actions field if field.association?
+ #generate_change_actions field if field.toggable?
end
+ end
+
+ module ActionMethods
+
def index_fields
- puffer_fields[:index] || Puffer::Fields.new
+ _index_fields
end
def show_fields
- puffer_fields[:show] || puffer_fields[:index] || Puffer::Fields.new
+ _show_fields.presence || _index_fields
end
def form_fields
- puffer_fields[:form] || Puffer::Fields.new
+ _form_fields
end
def create_fields
- puffer_fields[:create] || puffer_fields[:form] || Puffer::Fields.new
+ _create_fields.presence || _form_fields
end
def update_fields
- puffer_fields[:update] || puffer_fields[:form] || Puffer::Fields.new
- end
-
- def field name, options = {}
- puffer_fields[@puffer_option] ||= Puffer::Fields.new
- field = puffer_fields[@puffer_option].field(current_resource.model, name, options)
- #generate_association_actions field if field.association?
- #generate_change_actions field if field.toggable?
+ _update_fields.presence || _form_fields
end
end
end