Class: Kitsune::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/kitsune/builder.rb

Constant Summary collapse

TYPES =
[:check_box, :datetime_select, :password_field, :text_area, :text_field, :wysiwyg, :collection_select]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, &block) ⇒ Builder

Returns a new instance of Builder.



6
7
8
9
# File 'lib/kitsune/builder.rb', line 6

def initialize(resource, &block)
  @resource = resource
  self.instance_eval &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/kitsune/builder.rb', line 210

def method_missing(method, *args, &block)
  if TYPES.include?(method)
    add method, *args
  else
    raise Exception, "THIS IS WRONG@! #{method.to_s}"
  end
end

Class Method Details

.generate(resource, &block) ⇒ Object



75
76
77
# File 'lib/kitsune/builder.rb', line 75

def self.generate(resource, &block)
  Builder.new(resource, &block)
end

Instance Method Details

#add_faux_method(method_name) ⇒ Object



49
50
51
52
# File 'lib/kitsune/builder.rb', line 49

def add_faux_method method_name
 @resource.kitsune_admin[:faux_methods] ||= []
 @resource.kitsune_admin[:faux_methods]<<method_name
end

#always(*fields) ⇒ Object



100
101
102
103
104
105
# File 'lib/kitsune/builder.rb', line 100

def always(*fields)
  @resource.kitsune_admin[:always] ||= []
  fields.each do |field|
    @resource.kitsune_admin[:always] << field.to_sym
  end
end

#before_save(&block) ⇒ Object



31
32
33
# File 'lib/kitsune/builder.rb', line 31

def before_save &block
  @resource.kitsune_admin[:before_save] = block
end

#category(category) ⇒ Object



11
12
13
# File 'lib/kitsune/builder.rb', line 11

def category category
  @resource.kitsune_admin[:category] = category
end

#collection_select(field, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



132
133
134
# File 'lib/kitsune/builder.rb', line 132

def collection_select(field, collection, value_method, text_method, options = {}, html_options = {})
  add :collection_select, field, {:options => [collection, value_method, text_method, options, html_options]}
end

#columns(type = nil) ⇒ Object



83
84
85
# File 'lib/kitsune/builder.rb', line 83

def columns(type = nil)
  @resource.columns
end

#definitionsObject



79
80
81
# File 'lib/kitsune/builder.rb', line 79

def definitions
  yield # should be it's own builder
end

#disable(*types) ⇒ Object



87
88
89
90
91
# File 'lib/kitsune/builder.rb', line 87

def disable(*types)
	types.each do |type|
		@resource.kitsune_admin[:disabled] << type.to_sym
	end
end

#display(*fields) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/kitsune/builder.rb', line 159

def display(*fields)
  fields.each do |field|
    if field.is_a?(Hash)
      field.each do |key, value|
        set :display, key
        set_attributes(key, value)
      end
    else
      set :display, field
    end
  end
end

#display_and_edit(*args) ⇒ Object



154
155
156
157
# File 'lib/kitsune/builder.rb', line 154

def display_and_edit(*args)
  display *args
  edit *args
end

#edit(*fields) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/kitsune/builder.rb', line 191

def edit(*fields)
  fields.each do |field|
    if field.is_a?(Hash)
      field.each do |key, value|
        type = @resource.reflections[key.to_sym] ? :reflections : :edit
        set(type, key)
        set_attributes(key, value)
      end
    else
      type = @resource.reflections[field.to_sym] ? :reflections : :edit
      set type, field
    end
  end
end

#file(field) ⇒ Object



114
115
116
117
# File 'lib/kitsune/builder.rb', line 114

def file(field)
  add :file_field, field
  multipart
end

#image(field) ⇒ Object



119
120
121
122
# File 'lib/kitsune/builder.rb', line 119

def image(field)
  add :image_field, field
  multipart
end

#is_sti(field) ⇒ Object



149
150
151
152
# File 'lib/kitsune/builder.rb', line 149

def is_sti(field)
  @resource.kitsune_admin[:is_sti] = true
  @resource.kitsune_admin[:sti_column] = field
end

#linked(*fields) ⇒ Object

link to resource



107
108
109
110
111
112
# File 'lib/kitsune/builder.rb', line 107

def linked(*fields) # link to resource
  @resource.kitsune_admin[:linked] ||= []
  fields.each do |field|
    @resource.kitsune_admin[:linked] << field.to_sym
  end
end

#media(media) ⇒ Object



19
20
21
# File 'lib/kitsune/builder.rb', line 19

def media media
  @resource.kitsune_admin[:media] = media
end

#multipart(value = true) ⇒ Object



124
125
126
# File 'lib/kitsune/builder.rb', line 124

def multipart(value = true)
  @resource.kitsune_admin[:multipart] = value
end

#name(name) ⇒ Object



15
16
17
# File 'lib/kitsune/builder.rb', line 15

def name name
  @resource.kitsune_admin[:name] = name
end

#no_adminObject



67
68
69
# File 'lib/kitsune/builder.rb', line 67

def no_admin
  @resource.kitsune_admin[:no_admin] = true
end

#no_edit(*fields) ⇒ Object



206
207
208
# File 'lib/kitsune/builder.rb', line 206

def no_edit(*fields)
  set :no_edit, fields
end

#no_menuObject



35
36
37
# File 'lib/kitsune/builder.rb', line 35

def no_menu
	no_admin
end

#on_edit(&block) ⇒ Object



23
24
25
# File 'lib/kitsune/builder.rb', line 23

def on_edit &block
  @resource.kitsune_admin[:on_edit] = block
end

#on_new(&block) ⇒ Object



27
28
29
# File 'lib/kitsune/builder.rb', line 27

def on_new &block
  @resource.kitsune_admin[:on_new] = block
end

#order_by(order) ⇒ Object



39
40
41
42
# File 'lib/kitsune/builder.rb', line 39

def order_by(order)
 order = {order => :asc} unless order.is_a?(Hash)
 @resource.kitsune_admin[:order_by] = order
end

#remove_faux_methodsObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kitsune/builder.rb', line 54

def remove_faux_methods
  if @resource.kitsune_admin[:faux_methods]
    @resource.kitsune_admin[:faux_methods].each do |fm|
      @resource.send(:remove_method, fm) if @resource.methods.include?(fm.to_sym)
      if @resource.kitsune_admin[:edit][:fields]
        @resource.kitsune_admin[:edit][:fields].delete fm.to_sym if @resource.kitsune_admin[:edit][:fields].include?(fm.to_sym)
        @resource.kitsune_admin[:fields].delete fm.to_sym if @resource.kitsune_admin[:fields].keys.include?(fm)
      end
    end
    @resource.kitsune_admin[:faux_methods].clear
  end
end

#select(field, choices, options = {}, html_options = {}) ⇒ Object



128
129
130
# File 'lib/kitsune/builder.rb', line 128

def select(field, choices, options = {}, html_options = {})
  add :select, field, {:options => [choices, options, html_options]}
end

#set_attributes(field, attributes) ⇒ Object



185
186
187
188
189
# File 'lib/kitsune/builder.rb', line 185

def set_attributes(field, attributes)
  [attributes].flatten.each do |attribute|
    send(attribute.to_sym, field)
  end
end

#show(*fields) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/kitsune/builder.rb', line 172

def show(*fields)
  fields.each do |field|
    if field.is_a?(Hash)
      field.each do |key, value|
        set :show, key
        set_attributes(key, value)
      end
    else
      set :show, field
    end
  end
end

#sortable(*fields) ⇒ Object



93
94
95
96
97
98
# File 'lib/kitsune/builder.rb', line 93

def sortable(*fields)
  @resource.kitsune_admin[:sortable] ||= []
  fields.each do |field|
    @resource.kitsune_admin[:sortable] << field.to_sym
  end
end

#sti(field, options = {}) ⇒ Object



144
145
146
147
# File 'lib/kitsune/builder.rb', line 144

def sti(field, options = {})
  add :sti, field, {:options => options}
  is_sti field
end

#string(field) ⇒ Object



136
137
138
# File 'lib/kitsune/builder.rb', line 136

def string(field)
  add :text_field, field
end

#tabs(tabs) ⇒ Object



71
72
73
# File 'lib/kitsune/builder.rb', line 71

def tabs(tabs)
  @resource.kitsune_admin[:tabs] = tabs
end

#text(field) ⇒ Object



140
141
142
# File 'lib/kitsune/builder.rb', line 140

def text(field)
  add :text_area, field
end

#versionedObject



44
45
46
47
# File 'lib/kitsune/builder.rb', line 44

def versioned
 @resource.send(:versioned)
 @resource.kitsune_admin[:versioned] = true
end