Module: ResourcefulController::CrudDefinitions

Included in:
ClassMethods
Defined in:
lib/resourceful_controller/crud_definitions.rb

Instance Method Summary collapse

Instance Method Details

#default_methodsObject



21
22
23
# File 'lib/resourceful_controller/crud_definitions.rb', line 21

def default_methods
  [:index, :new, :create, :show, :edit, :update, :destroy]
end

#define_createObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/resourceful_controller/crud_definitions.rb', line 37

def define_create
  define_method :create do
    instance_variable_set(:"@#{resource_name}", resource_class.new(params[:"#{resource_name}"]))
    if instance_variable_get(:"@#{resource_name}").save
      send(:redirect_to, show_path, :notice => "#{resource_name.titleize.capitalize} successfully created")
    else
      render :new
    end
  end
end

#define_crud_methodsObject



3
4
5
# File 'lib/resourceful_controller/crud_definitions.rb', line 3

def define_crud_methods
  methods.each { |method| send(:"define_#{method}") }
end

#define_destroyObject



71
72
73
74
75
76
77
# File 'lib/resourceful_controller/crud_definitions.rb', line 71

def define_destroy
  define_method :destroy do
    resource = _find_query.call(params)
    resource.destroy
    redirect_to index_path, :notice => "#{resource_name.titleize.capitalize} successfully deleted"
  end
end

#define_editObject



54
55
56
57
58
# File 'lib/resourceful_controller/crud_definitions.rb', line 54

def define_edit
  define_method :edit do
    instance_variable_set(:"@#{resource_name}", _find_query.call(params))
  end
end

#define_indexObject



25
26
27
28
29
# File 'lib/resourceful_controller/crud_definitions.rb', line 25

def define_index
  define_method :index do
    instance_variable_set(:"@#{resources_name}", _index_query.call(params))
  end
end

#define_newObject



31
32
33
34
35
# File 'lib/resourceful_controller/crud_definitions.rb', line 31

def define_new
  define_method :new do
    instance_variable_set(:"@#{resource_name}", resource_class.new)
  end
end

#define_showObject



48
49
50
51
52
# File 'lib/resourceful_controller/crud_definitions.rb', line 48

def define_show
  define_method :show do
    instance_variable_set(:"@#{resource_name}", _find_query.call(params))
  end
end

#define_updateObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/resourceful_controller/crud_definitions.rb', line 60

def define_update
  define_method :update do
    instance_variable_set(:"@#{resource_name}", _find_query.call(params))
    if instance_variable_get(:"@#{resource_name}").update_attributes(params[:"#{resource_name}"])
      redirect_to show_path, :notice => "#{resource_name.titleize.capitalize} successfully updated"
    else
      render :edit
    end
  end
end

#excepted_methodsObject



13
14
15
# File 'lib/resourceful_controller/crud_definitions.rb', line 13

def excepted_methods
  Array.wrap(@options[:except])
end

#methodsObject



7
8
9
10
11
# File 'lib/resourceful_controller/crud_definitions.rb', line 7

def methods
  methods = default_methods - excepted_methods
  methods = methods & specified_methods if specified_methods.present?
  return methods
end

#specified_methodsObject



17
18
19
# File 'lib/resourceful_controller/crud_definitions.rb', line 17

def specified_methods
  Array.wrap(@options[:only])
end