Module: CckForms::ParameterTypeClass::Base

Extended by:
ActiveSupport::Concern
Included in:
Album, Boolean, Checkboxes, Date, DateRange, DateTime, Enum, File, Float, Integer, Map, NumberRange, Phones, String, StringCollection, Text, Time, WorkHours
Defined in:
lib/cck_forms/parameter_type_class/base.rb

Overview

Base module for all field types. Is included in type classes like Album, Image etc.

class CckForms::ParameterTypeClass::NewType
  include CckForms::ParameterTypeClass::Base
end

Standalone usage of real classes:

field :cover_photo, type: CckForms::ParameterTypeClass::Image
field :gallery,     type: CckForms::ParameterTypeClass::Album
field :description, type: CckForms::ParameterTypeClass::Text

Base module includes:

1) URL helpers like edit_article_path accessible via include Rails.application.routes.url_helpers;

2) methods cck_param & value returning current CCK parameter (module CckForms::*::Parameter) and his current value;

3) methods with_cck_param(param) do ..., with_value(value) do ... and with_cck_param_and_value(param, value) do ...
   which set the corresponding values for the block duration;

4) dynamic method (via method_missing & respond_to?) ..._with_value(value, args*) which is basically the same as
   with_value do... but for one method invocation only: foo_with_value(v) <=> with_value(v) { foo }

5) utility set_value_in_hash(hash) placing value in hash[:value];

6) utilities to get HTML ID: form(_builder)?_name_to_id;

7) methods to be consumed by the type classes:

     self.code  - type code (e.g. rich_text for CckForms::ParameterType::RichText)
     self.name  - type name (e.g. "A text string")

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_type_classesObject

Load all type classes TODO: relies on all classes to reside in this class’ directory



130
131
132
133
134
135
136
137
138
139
# File 'lib/cck_forms/parameter_type_class/base.rb', line 130

def self.load_type_classes
  return if @type_classes_loaded

  path = File.dirname(__FILE__)
  Dir[path + '/*.rb'].each do |filename|
    require_dependency filename unless filename.ends_with? '/base.rb'
  end

  @type_classes_loaded = true
end

Instance Method Details

#build_form(form_builder, options) ⇒ Object

Builds an edit form in HTML. By default an input:text (value can be set via options).



205
206
207
208
# File 'lib/cck_forms/parameter_type_class/base.rb', line 205

def build_form(form_builder, options)
  set_value_in_hash options
  form_builder.text_field :value, options
end

#default_url_optionsObject

For Rails.application.routes.url_helpers



234
235
236
# File 'lib/cck_forms/parameter_type_class/base.rb', line 234

def default_url_options
  {}
end

#demongoize_valueObject



244
245
246
# File 'lib/cck_forms/parameter_type_class/base.rb', line 244

def demongoize_value
  self.class.demongoize_value value, self
end

#demongoize_value!Object



248
249
250
# File 'lib/cck_forms/parameter_type_class/base.rb', line 248

def demongoize_value!
  self.value = demongoize_value
end

#mongoizeObject

Transforms the value into MongoDB form. By default returns the value itself.



240
241
242
# File 'lib/cck_forms/parameter_type_class/base.rb', line 240

def mongoize
  value
end

#search(selectable, field, query) ⇒ Object

Transforms DSL-like query (specific to each type) into Mongoid query. By default use simple where(field: query.to_s).



229
230
231
# File 'lib/cck_forms/parameter_type_class/base.rb', line 229

def search(selectable, field, query)
  selectable.where(field => query.to_s)
end

#to_diff_value(options = nil) ⇒ Object

Was-became HTML for admin panels etc. (like a type image with map preview for Map).



223
224
225
# File 'lib/cck_forms/parameter_type_class/base.rb', line 223

def to_diff_value(options = nil)
  to_html options
end

#to_html(options = nil) ⇒ Object

HTML form of a type class. By default to_s.



212
213
214
# File 'lib/cck_forms/parameter_type_class/base.rb', line 212

def to_html(options = nil)
  to_s options
end

#to_s(options = nil) ⇒ Object

Redefines to allow passing of options. By default, call to_s on the current value.



218
219
220
# File 'lib/cck_forms/parameter_type_class/base.rb', line 218

def to_s(options = nil)
  value.to_s
end

#valid_valuesObject

If valid_values is empty and valid_values_class is not, extracts all values from this class into valid_values. Makes use of ActiveRecord-like method .all for this



193
194
195
196
197
198
199
200
201
# File 'lib/cck_forms/parameter_type_class/base.rb', line 193

def valid_values
  @valid_values ||= begin
    if vv_class = valid_values_class
      valid_values = {}
      vv_class.all.each { |valid_value_object| valid_values[valid_value_object.id] = valid_value_object.to_s }
      valid_values
    end
  end
end

#valid_values_as_stringObject

Generates valid values as a comma-separated string: “georgian: грузинская, albanian: албанская” (for HTML puproses)



158
159
160
# File 'lib/cck_forms/parameter_type_class/base.rb', line 158

def valid_values_as_string
  valid_values_enum.map { |enum| "#{enum[1]}: #{enum[0]}" }.join "\n"
end

#valid_values_as_string=(string) ⇒ Object

Convert HTML form back into Hash again:

= f.text_field :valid_values_as_string


164
165
166
167
168
169
170
171
# File 'lib/cck_forms/parameter_type_class/base.rb', line 164

def valid_values_as_string=(string)
  new_valid_values = {}
  string.split("\n").reject { |line| line.blank? }.each do |line|
    splitted = line.split(':', 2)
    new_valid_values[splitted[0].strip] = splitted[1].strip if splitted.length == 2 and splitted[0].present?
  end
  self.valid_values = new_valid_values
end

#valid_values_classObject

“City” -> City



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/cck_forms/parameter_type_class/base.rb', line 174

def valid_values_class
  if valid_values_class_name.present?
    if valid_values_class_name.is_a? Class
      valid_values_class_name
    else # raises exception if this is not a string
      valid_values_class_name.constantize
    end
  else
    nil
  end
end

#valid_values_class?Boolean

Is valid_values_class exist at all?

Returns:



187
188
189
# File 'lib/cck_forms/parameter_type_class/base.rb', line 187

def valid_values_class?
  not valid_values_class.nil?
end

#valid_values_enumObject

Generates valid_values in form consumable to SELECT helper builders: [[key1, value1], [key2, value2], …]



146
147
148
149
150
151
152
153
154
155
# File 'lib/cck_forms/parameter_type_class/base.rb', line 146

def valid_values_enum
  valid_values = self.valid_values
  return [] if valid_values.blank?
  result = []
  method_for_enumerating = valid_values.is_a?(Array) ? :each_with_index : :each_pair
  valid_values.send(method_for_enumerating) do |key, value|
    result.push [value, key]
  end
  result
end