Class: ModelBase

Inherits:
Object show all
Includes:
AMFSerializable
Defined in:
lib/mrpin/core/base/model_base.rb

Overview

todo:review

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AMFSerializable

#encode_amf

Class Method Details

.admin_groupObject



15
16
17
# File 'lib/mrpin/core/base/model_base.rb', line 15

def self.admin_group
  self.name.underscore.split('_').first
end

.configure_admin_edit(context, field_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/mrpin/core/base/model_base.rb', line 69

def self.configure_admin_edit(context, field_name)
  case field_name
    when :_type, :created_at, :updated_at
      context.configure field_name do
        visible false
      end
    else
      context.configure field_name
  end
end

.configure_admin_list(context, field_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mrpin/core/base/model_base.rb', line 20

def self.configure_admin_list(context, field_name)
  model_class = get_class_by_name(context.abstract_model.model_name)

  is_mongoid_field          = false
  is_mongoid_relation_field = false

  if model_class.include?(Mongoid::Document)
    if model_class.fields.include?(field_name.to_s)
      is_mongoid_field = true
    end

    if model_class.relations.keys.include?(field_name.to_s)
      is_mongoid_relation_field = true
    end
  end

  if is_mongoid_field && model_class.fields[field_name.to_s].type == Mongoid::Boolean
    context.configure field_name
  elsif is_mongoid_relation_field
    context.configure field_name
  else
    case field_name
      when :_type
        context.configure field_name do
          visible false
        end
      when /_at\z/
        context.configure field_name do
          pretty_value do
            Time.at(value).to_s(:short) unless value.nil?
          end
          read_only true
        end

      when /\Aurl_/
        context.configure field_name do
          pretty_value do
            bindings[:view].link_to(bindings[:view].tag(:img, {src: value, height: 25, width: 25}), value.html_safe, target: '_blank') unless value.nil?
          end
        end
      else
        context.configure field_name do
          pretty_value { value }
        end
    end
  end
end

.configure_admin_show(context, field_name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mrpin/core/base/model_base.rb', line 81

def self.configure_admin_show(context, field_name)
  case field_name

    when :_type
      context.configure field_name do
        visible false
      end

    when /_at\z/
      context.configure field_name do
        pretty_value do
          Time.at(value).to_s(:short)
        end
      end

    when /\Aurl_/
      context.configure field_name do
        pretty_value do
          bindings[:view].link_to(bindings[:view].tag(:img, {src: value, height: 100, width: 100}), value.html_safe, target: '_blank') unless value.nil?
        end
      end

    else
      context.configure field_name
  end
end

.filters_for_admin_list(section) ⇒ Object



114
115
116
# File 'lib/mrpin/core/base/model_base.rb', line 114

def self.filters_for_admin_list(section)
  section.filters []
end

.init_scopesObject



123
124
125
# File 'lib/mrpin/core/base/model_base.rb', line 123

def self.init_scopes

end

.need_show_for_admin_user?(user) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/mrpin/core/base/model_base.rb', line 133

def self.need_show_for_admin_user?(user)
  user.role == EAdminUserRole::EAUR_ADMIN
end

.sort_by_for_admin_list(section) ⇒ Object



109
110
111
# File 'lib/mrpin/core/base/model_base.rb', line 109

def self.sort_by_for_admin_list(section)
  section.sort_by :_id
end

Instance Method Details

#clone_modelObject



215
216
217
# File 'lib/mrpin/core/base/model_base.rb', line 215

def clone_model
  self.class.new(self.as_json)
end

#create_infoObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mrpin/core/base/model_base.rb', line 156

def create_info
  return nil if class_info.nil?

  result = class_info.new

  class_info.attributes.each do |attribute_name|
    next unless self.respond_to? attribute_name

    attribute_value = get_attribute_value(attribute_name)

    result.send("#{attribute_name}=", attribute_value)
  end

  result
end

#create_info_with_checkObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/mrpin/core/base/model_base.rb', line 173

def create_info_with_check
  return nil if class_info.nil?

  result = class_info.new

  class_info.attributes.each do |attribute_name|
    if (self.respond_to?(attribute_name) && self.has_attribute?(attribute_name)) || attribute_name.to_s == 'id'

      attribute_value = get_attribute_value(attribute_name)

      result.send("#{attribute_name}=", attribute_value)
    end
  end

  result
end