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.4.27 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.4 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.26 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.3 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.25 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.2 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.24 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.1 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.23 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.22 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.0 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.21 app/view_models/workarea/application_view_model.rb
workarea-core-3.5.0.beta.1 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.20 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.19 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.18 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.17 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.16 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.15 app/view_models/workarea/application_view_model.rb
workarea-core-3.4.14 app/view_models/workarea/application_view_model.rb