motion-prime/elements/base_element.rb in motion-prime-0.4.2 vs motion-prime/elements/base_element.rb in motion-prime-0.4.3

- old
+ new

@@ -1,15 +1,18 @@ motion_require '../helpers/has_normalizer' motion_require '../helpers/has_style_chain_builder' +motion_require '../helpers/has_class_factory' module MotionPrime class BaseElement # MotionPrime::BaseElement is container for UIView class elements with options. # Elements are located inside Sections include ::MotionSupport::Callbacks include HasNormalizer include HasStyleChainBuilder + include HasClassFactory + extend HasClassFactory attr_accessor :options, :section, :name, :view_class, :view, :view_name, :styles, :screen delegate :observing_errors?, :has_errors?, :errors_observer_fields, :observing_errors_for, to: :section, allow_nil: true define_callbacks :render @@ -18,22 +21,22 @@ @options = options @screen = options[:screen] @section = options[:section] @name = options[:name] @block = options[:block] - @view_class = options[:view_class] || "UIView" + @view_class = options[:view_class] || 'UIView' @view_name = self.class_name_without_kvo.demodulize.underscore.gsub(/(_draw)?_element/, '') end def render(options = {}, &block) run_callbacks :render do render!(&block) end end def render!(&block) - screen.add_view view_class.constantize, computed_options do |view| + screen.add_view class_factory(view_class), computed_options do |view| @view = view block.try(:call, view, self) end end @@ -95,18 +98,20 @@ suffixes = {common: [], specific: []} # following example in Prime::TableSection#cell_styles # form element/cell: <base|user>_form_field, <base|user>_form_string_field, user_form_field_email # table element/cell: <base|categories>_table_cell, categories_table_title - section.section_styles.each { |type, values| base_styles[type] += values } if section.section_styles - if %w[base table_view_cell].exclude?(@view_name.to_s) + if section.section_styles + section.section_styles.each { |type, values| base_styles[type] += values } + end + if %w[base table_view_cell].exclude?(@view_name) # form element: _input # table element: _image suffixes[:common] << @view_name.to_sym suffixes[:common] << :"#{@view_name}_with_errors" if has_errors end - if name && name.to_s != @view_name.to_s + if name && name.to_s != @view_name # form element: _input # table element: _icon suffixes[:specific] << name.to_sym suffixes[:specific] << :"#{name}_with_errors" if has_errors end @@ -116,22 +121,22 @@ # table element: base_table_cell_image common_styles = if suffixes[:common].any? build_styles_chain(base_styles[:common], suffixes[:common]) elsif suffixes[:specific].any? build_styles_chain(base_styles[:common], suffixes[:specific]) - elsif @view_name.to_s == 'table_view_cell' + elsif @view_name == 'table_view_cell' base_styles[:common] end @styles += Array.wrap(common_styles) # form cell: user_form_field, user_form_string_field, user_form_field_email # form element: user_form_field_text_field, user_form_string_field_text_field, user_form_field_email_text_field # table cell: categories_table_cell, categories_table_title # table element: categories_table_cell_image, categories_table_title_image specific_base_common_suffix_styles = if suffixes[:common].any? build_styles_chain(base_styles[:specific], suffixes[:common]) - elsif suffixes[:specific].empty? && @view_name.to_s == 'table_view_cell' + elsif suffixes[:specific].empty? && @view_name == 'table_view_cell' base_styles[:specific] end @styles += Array.wrap(specific_base_common_suffix_styles) # form element: user_form_field_input, user_form_string_field_input, user_form_field_email_input # table element: categories_table_cell_icon, categories_table_title_icon @@ -142,23 +147,21 @@ @styles << [section.name, name].compact.join('_').to_sym end # custom style (from options or block options), using for TableViews as well custom_styles = style_sources.map do |source| normalize_object(source.delete(:styles), section) - end.compact.flatten + end.flatten @styles += custom_styles - # puts @view_class.to_s + @styles.inspect, '' + #puts @view_class.to_s + @styles.inspect, '' end class << self def factory(type, options = {}) - class_name = "#{type.classify}Element" - options.merge!({view_class: "UI#{type.classify}"}) - if MotionPrime.const_defined?(class_name) - "MotionPrime::#{class_name}".constantize.new(options) - else - self.new(options) - end + element_class = class_factory("#{type}_element", true) || self + view_class_name = camelize_factory("ui_#{type}") + + options.merge!(view_class: view_class_name) + element_class.new(options) end def before_render(method_name) set_callback :render, :before, method_name end def after_render(method_name) \ No newline at end of file