Module: Adminpanel::ResourceGeneratorHelper

Included in:
MigrationGenerator, ResourceGenerator
Defined in:
lib/generators/adminpanel/resource/resource_generator_helper.rb

Instance Method Summary collapse

Instance Method Details

#assign_attributes_variables(attribute) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 23

def assign_attributes_variables(attribute)
  @attr_field = attribute.split(":").first
  if attribute.split(":").second.nil?
    @attr_type = "string"
  else
    @attr_type = attribute.split(":").second
  end
end

#associationsObject



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 159

def associations
  association = ""
  fields.each do |attribute|
    assign_attributes_variables(attribute)
    if @attr_type == "belongs_to"
      association = "#{association}#{belongs_to_association(@attr_field)}"
    elsif @attr_type == "has_many" || @attr_type == "has_many_through"
      association = "#{association}#{has_many_association(@attr_field)}"
    end

  end
  association
end

#attribute_hash(name, type, model = '') ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 119

def attribute_hash(name, type, model = '')
  if model != ''
    model = model_type(model) + ",\n"
  end
  "{\n" +
    indent("'#{name}'" + " => {\n", 2) +
      indent(form_type(type), 4) + ",\n" +
      indent(label_type, 4) + ",\n" +
      indent(placeholder_type, 4) + ",\n" +
      indent(model, 4) +
    indent("}\n", 2) +
  '}'
end

#belongs_to_association(field) ⇒ Object



173
174
175
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 173

def belongs_to_association(field)
  "belongs_to :#{field.singularize.downcase}\n\t\t"
end

#belongs_to_field(resource) ⇒ Object



11
12
13
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 11

def belongs_to_field(resource)
  "#{resource.singularize.downcase}_id"
end

#belongs_to_form_hashObject



111
112
113
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 111

def belongs_to_form_hash
  attribute_hash(belongs_to_field(@attr_field), 'belongs_to', resource_class_name(@attr_field))
end

#boolean_form_hashObject



99
100
101
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 99

def boolean_form_hash
  attribute_hash(@attr_field, 'boolean')
end

#camelized_resourceObject



54
55
56
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 54

def camelized_resource
  resource_name.camelize
end

#datepicker_form_hashObject



103
104
105
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 103

def datepicker_form_hash
  attribute_hash(@attr_field, 'datepicker')
end

#file_field_form_hashObject



107
108
109
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 107

def file_field_form_hash
  attribute_hash(gallery_name.pluralize, 'adminpanel_file_field')
end

#float_form_hashObject



87
88
89
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 87

def float_form_hash
  attribute_hash(@attr_field, 'text_field')
end

#form_type(type) ⇒ Object



133
134
135
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 133

def form_type(type)
  "'type' => '#{type}'"
end


50
51
52
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 50

def gallery_name
  "#{resource_name}file" #ex: postfile
end

#get_attribute_hashObject



76
77
78
79
80
81
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 76

def get_attribute_hash
  fields.map do |attribute|
    assign_attributes_variables(attribute)
    send(@attr_type + '_form_hash')
  end.join(", \n")
end


184
185
186
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 184

def get_gallery
  return "\n\t\tmount_images :#{gallery_name.pluralize}\n"
end

#has_associations?Boolean



149
150
151
152
153
154
155
156
157
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 149

def has_associations?
  fields.each do |attribute|
    assign_attributes_variables(attribute)
    if @attr_type == "images" || @attr_type == "belongs_to" || @attr_type == "has_many" || @attr_type == "has_many_through"
      return true
    end
  end
  return false
end

#has_gallery?Boolean



42
43
44
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 42

def has_gallery?
  !options[:'skip-gallery']
end

#has_many_association(field) ⇒ Object



177
178
179
180
181
182
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 177

def has_many_association(field)
  return "# has_many :categorizations\n\t\t" +
  "# has_many :#{@attr_field}, " +
  ":through => :categorizations, " +
  ":dependent => :destroy\n\t\t"
end

#has_many_field(resource) ⇒ Object



15
16
17
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 15

def has_many_field(resource)
  "#{resource.singularize.downcase}_ids"
end

#has_many_form_hashObject



115
116
117
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 115

def has_many_form_hash
  attribute_hash(has_many_field(resource_class_name(@attr_field)), 'has_many', 'has_many model')
end

#integer_form_hashObject



95
96
97
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 95

def integer_form_hash
  attribute_hash(@attr_field, 'number_field')
end

#is_a_resource?Boolean



32
33
34
35
36
37
38
39
40
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 32

def is_a_resource?
  fields.each do |attribute|
    assign_attributes_variables(attribute)
    if @attr_type != 'belongs_to'
      return true
    end
  end
  false
end

#label_typeObject



137
138
139
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 137

def label_type
  "'label' => '#{@attr_field}'"
end

#model_type(type) ⇒ Object



145
146
147
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 145

def model_type(type)
  "'model' => 'Adminpanel::#{type}'"
end

#placeholder_typeObject



141
142
143
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 141

def placeholder_type
  "'placeholder' => '#{@attr_field}'"
end

#pluralized_nameObject



58
59
60
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 58

def pluralized_name
  "#{resource_name.pluralize}"
end

#resource_class_name(resource) ⇒ Object



19
20
21
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 19

def resource_class_name(resource)
  "#{resource.singularize.capitalize}"
end

#resource_nameObject



46
47
48
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 46

def resource_name
  name.singularize.downcase #normalize name to downcase and singular
end

#setup_is_found?Boolean



3
4
5
6
7
8
9
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 3

def setup_is_found?
  if Dir.exists?('config') && Dir.exists?('config/initializers') && File.exists?('config/initializers/adminpanel_setup.rb')
    true
  else
    false
  end
end

#string_form_hashObject



83
84
85
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 83

def string_form_hash
  attribute_hash(@attr_field, 'text_field')
end

#symbolized_attributesObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 62

def symbolized_attributes
  fields.map do |attribute|
    assign_attributes_variables(attribute)
    case @attr_type
      when 'belongs_to'
        ':' + belongs_to_field(@attr_field)
      when 'has_many'
        has_many_field(@attr_field) + ': []'
      else
      ":#{attribute.split(':').first}"
    end
  end.join(",\n")
end

#text_form_hashObject



91
92
93
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 91

def text_form_hash
  attribute_hash(@attr_field ,'wysiwyg_field')
end