Sha256: a37e03e0d0e7af601579dfa777a1191312071149472c0ddb95131bf604a73eae

Contents?: true

Size: 1.53 KB

Versions: 62

Compression:

Stored size: 1.53 KB

Contents

module Workarea
  class ApplicationViewModel
    attr_reader :model, :options

    def self.wrap(input, options = {})
      if input.is_a?(Enumerable)
        input.map { |i| new(i, options) }
      else
        new(input, options)
      end
    end

    def initialize(model = nil, options = {})
      @model = model
      @options = if options.respond_to?(:to_unsafe_h)
                   options.to_unsafe_h.with_indifferent_access
                 else
                   options.to_h.with_indifferent_access
                 end
    end

    def method_missing(method, *args, &block)
      if model && model.respond_to?(method)
        # Define a method so the next call is faster
        self.class.send(:define_method, method) do |*args, &blok|
          model.send(method, *args, &blok)
        end

        send(method, *args, &block)
      else
        super
      end

    rescue NoMethodError => no_method_error
      super if no_method_error.name == method
      raise no_method_error
    end

    def respond_to_missing?(method_name, include_private = false)
      super || (model && model.respond_to?(method_name))
    end

    def to_param
      model.to_param
    end

    def to_model
      model
    end

    def present?
      model.present?
    end

    def blank?
      model.blank?
    end

    def nil?
      model.nil?
    end

    def ==(other)
      (other.class == self.class && other.id == id) || other == model
    end

    def translate(key, options = {})
      I18n.translate(key, options)
    end
    alias :t :translate
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.26 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.45 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.25 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.23 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.44 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.22 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.43 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.21 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.42 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.20 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.41 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.19 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.40 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.18 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.39 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.17 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.38 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.16 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.37 app/view_models/workarea/application_view_model.rb