Class: Hippo::Screen::Definition

Inherits:
Object
  • Object
show all
Includes:
Concerns::AttrAccessorWithDefault
Defined in:
lib/hippo/screen/definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, extension_id) ⇒ Definition

Returns a new instance of Definition.


22
23
24
25
26
# File 'lib/hippo/screen/definition.rb', line 22

def initialize(id, extension_id)
    self.identifier = id
    @extension_id = extension_id
    @extension    = extension_id.underscore.camelize
end

Instance Attribute Details

#extension_idObject

Returns the value of attribute extension_id


20
21
22
# File 'lib/hippo/screen/definition.rb', line 20

def extension_id
  @extension_id
end

Instance Method Details

#as_jsonObject


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hippo/screen/definition.rb', line 58

def as_json
    {
        id:    identifier,
        title: title,
        icon:  icon,
        model: model_class,
        view:  view_class,
        access: model_access,
        group_id: group_id,
        extension: extension,
        description: description,
        asset: asset_path
    }
end

#asset_pathObject


54
55
56
# File 'lib/hippo/screen/definition.rb', line 54

def asset_path
    (asset && asset =~ /\//) ? asset : "#{@extension_id}/screens/#{asset || identifier}"
end

#groupObject


28
29
30
# File 'lib/hippo/screen/definition.rb', line 28

def group
    GROUPS[@group_id]
end

#has_file_matching?(pattern) ⇒ Boolean

Returns:

  • (Boolean)

32
33
34
# File 'lib/hippo/screen/definition.rb', line 32

def has_file_matching?(pattern)
    Pathname.glob(root_path.join(pattern)).any?
end

#modelObject


42
43
44
45
# File 'lib/hippo/screen/definition.rb', line 42

def model
    return nil if @model_class.blank?
    @model_class.safe_constantize || (@extension_id.camelize + '::' + @model_class).safe_constantize
end

#root_pathObject


36
37
38
39
40
# File 'lib/hippo/screen/definition.rb', line 36

def root_path
    ext = Hippo::Extensions.for_identifier(@extension_id)
    raise "Unable to find extension '#{@extension_id}' for screen group" unless ext
    ext.root_path.join('client', url_prefix, identifier)
end

#to_jsonObject


73
74
75
# File 'lib/hippo/screen/definition.rb', line 73

def to_json
    Oj.dump(as_json, mode: :compat)
end

#viewable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)

47
48
49
50
51
52
# File 'lib/hippo/screen/definition.rb', line 47

def viewable_by?(user)
    if roles.present?
        return (roles & user.role_names).any?
    end
    model.nil? || user.can_read?(self)
end