Module: AoCrudify::ClassMethods

Defined in:
lib/ao_crudify/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#crudify(model_name, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ao_crudify/class_methods.rb', line 13

def crudify(model_name, options = {})

  options = ::AoCrudify::Base.default_options(model_name).merge(options)

  singular_name = options[:singular_name]
  plural_name   = options[:plural_name]
  class_name    = options[:class_name]
  klass         = class_name.constantize

  options[:paginate] = options[:paginate] && klass.respond_to?(:paginate)

  module_eval %(

    # (Just a comment!)

    before_filter :find_#{singular_name},
                  :only => [:update, :destroy, :edit, :show]

    before_filter :set_crud_options

    def set_crud_options
      @crud_options ||= #{options.inspect}
    end

    def what
      @what ||= '#{options[:title_attribute]}'
    end

    def index
    end

    def show
    end

    def new
      @#{singular_name} = #{class_name}.new
    end


    def create
      @instance = @#{singular_name} = #{class_name}.new(params[:#{singular_name}])
      return if before_create === false
      if @instance.save
        successful_create
      else
        failed_create
      end
    end

    def edit

    end

    def update
      return if before_update === false
      if @#{singular_name}.update_attributes(params[:#{singular_name}])
        successful_update
      else
        failed_update
      end
    end

    def destroy
      return if before_destroy === false
      if @#{singular_name}.destroy
        successful_destroy
      else
        failed_destroy
      end
    end

    # Finds one single result based on the id params.
    def find_#{singular_name}
      set_instance(#{class_name}.find(params[:id]))
    end

    def paginate_all_#{plural_name}
      set_collection(#{class_name}.paginate paginate_options.merge(conditions))
    end

    def find_all_#{plural_name}
      set_collection(#{class_name}.all conditions)
    end

    def paginate_options
      @paginate_options ||= {
        :page => params[:page],
        :per_page => params[:per_page],
        :order_by => :#{options[:order_by]},
        :direction => :#{options[:direction]}
      }
    end

    def conditions
      @conditions ||= @crud_options[:conditions]
    end

    def set_instance(record)
      @instance = @#{singular_name} = record
    end

    def set_collection(records)
      @collection = @#{plural_name} = records
    end

  )


  if options[:paginate]
    module_eval %(
      def index
        paginate_all_#{plural_name}
      end
    )
  else
    module_eval %(
      def index
        find_all_#{plural_name}
      end
    )
  end
end