Class: TranslationCms::Api::Base

Inherits:
JsonApiClient::Resource
  • Object
show all
Defined in:
lib/translation_cms/api/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all!(*args) ⇒ Object



29
30
31
32
33
# File 'lib/translation_cms/api/base.rb', line 29

def all!(*args)
  Rails.cache.fetch("<crm_responce>#{name}.all@#{args.to_s.hash}", expires_in: 1.hour) do
    all
  end
end

.custom_scope!(val) ⇒ Object

define resource custom scope sample [api_endpoint]//[resource]



25
26
27
# File 'lib/translation_cms/api/base.rb', line 25

def custom_scope!(val)
  self.custom_scope = val
end

.default_attributesObject



49
50
51
# File 'lib/translation_cms/api/base.rb', line 49

def default_attributes
  resource_type ? { type: resource_type.to_s } : super
end

.destroy(params = {}) ⇒ Object



79
80
81
82
83
84
# File 'lib/translation_cms/api/base.rb', line 79

def destroy(params = {})
  rec = new(params)
  rec.mark_as_persisted!
  rec.destroy if params[:id].present?
  rec
end

.dummy_resource!(key, base_class = JsonApiClient::Resource, &block) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/translation_cms/api/base.rb', line 64

def dummy_resource!(key, base_class = JsonApiClient::Resource, &block)
  dummy_class = key.singularize.classify
  return if const_defined?(dummy_class)

  # define dummy constant
  const_set(dummy_class, Class.new(base_class, &block))
end

.parsed!(key, parse_as = :date) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/translation_cms/api/base.rb', line 53

def parsed!(key, parse_as = :date)
  define_method "parsed_#{key}" do
    return nil if self[key].blank?

    case parse_as.to_s
    when 'date'
      self["parsed_#{key}"] ||= Time.zone.parse(self[key])
    end
  end
end

.path_with_custom_scope(*args) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/translation_cms/api/base.rb', line 40

def path_with_custom_scope(*args)
  if custom_scope
    [custom_scope.to_s, path_without_custom_scope(*args)].join('/')
  else
    path_without_custom_scope(*args)
  end
end

.resource_type!(val) ⇒ Object

define custom resource type (serialize only!)



36
37
38
# File 'lib/translation_cms/api/base.rb', line 36

def resource_type!(val)
  self.resource_type = val
end

.update(params = {}) ⇒ Object



72
73
74
75
76
77
# File 'lib/translation_cms/api/base.rb', line 72

def update(params = {})
  rec = new(params)
  rec.mark_as_persisted!
  rec.update if params[:id].present?
  rec
end

Instance Method Details

#method_missing_with_translations_will_change(method, *args, &block) ⇒ Object

we need to implement [attribute]_translations_will_change! method but who cares?



15
16
17
18
19
# File 'lib/translation_cms/api/base.rb', line 15

def method_missing_with_translations_will_change(method, *args, &block)
  return if method =~ /_translations_will_change!$/

  method_missing_without_translations_will_change(method, *args, &block)
end