Class: ApplicationType

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::MassAssignmentSecurity, RestApi::Cacheable
Defined in:
app/models/application_type.rb

Defined Under Namespace

Classes: NotFound

Constant Summary collapse

PROTECTED_TAGS =
[:new, :premium, :blacklist, :featured]
CartridgeSpecInvalid =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ ApplicationType

Returns a new instance of ApplicationType.



33
34
35
36
37
38
# File 'app/models/application_type.rb', line 33

def initialize(attributes={}, persisted=false)
  attributes.each do |name,value|
    send("#{name}=", value)
  end
  @persisted = persisted
end

Instance Attribute Details

#cartridgesObject

Returns the value of attribute cartridges.



21
22
23
# File 'app/models/application_type.rb', line 21

def cartridges
  @cartridges
end

#cartridges_specObject

Returns the value of attribute cartridges_spec.



21
22
23
# File 'app/models/application_type.rb', line 21

def cartridges_spec
  @cartridges_spec
end

#descriptionObject

Returns the value of attribute description.



20
21
22
# File 'app/models/application_type.rb', line 20

def description
  @description
end

#display_nameObject

Returns the value of attribute display_name.



20
21
22
# File 'app/models/application_type.rb', line 20

def display_name
  @display_name
end

#help_topicsObject

Returns the value of attribute help_topics.



24
25
26
# File 'app/models/application_type.rb', line 24

def help_topics
  @help_topics
end

#idObject

Returns the value of attribute id.



20
21
22
# File 'app/models/application_type.rb', line 20

def id
  @id
end

#initial_git_branchObject

Returns the value of attribute initial_git_branch.



21
22
23
# File 'app/models/application_type.rb', line 21

def initial_git_branch
  @initial_git_branch
end

#initial_git_urlObject

Returns the value of attribute initial_git_url.



21
22
23
# File 'app/models/application_type.rb', line 21

def initial_git_url
  @initial_git_url
end

#learn_more_urlObject

Returns the value of attribute learn_more_url.



23
24
25
# File 'app/models/application_type.rb', line 23

def learn_more_url
  @learn_more_url
end

#licenseObject

Returns the value of attribute license.



22
23
24
# File 'app/models/application_type.rb', line 22

def license
  @license
end

#license_urlObject

Returns the value of attribute license_url.



22
23
24
# File 'app/models/application_type.rb', line 22

def license_url
  @license_url
end

#priorityObject

Returns the value of attribute priority.



25
26
27
# File 'app/models/application_type.rb', line 25

def priority
  @priority
end

#scalableObject Also known as: scalable?

Returns the value of attribute scalable.



26
27
28
# File 'app/models/application_type.rb', line 26

def scalable
  @scalable
end

#sourceObject

Returns the value of attribute source.



28
29
30
# File 'app/models/application_type.rb', line 28

def source
  @source
end

#tagsObject

Returns the value of attribute tags.



23
24
25
# File 'app/models/application_type.rb', line 23

def tags
  @tags
end

#versionObject

Returns the value of attribute version.



20
21
22
# File 'app/models/application_type.rb', line 20

def version
  @version
end

#websiteObject

Returns the value of attribute website.



22
23
24
# File 'app/models/application_type.rb', line 22

def website
  @website
end

Class Method Details

.all(*arguments) ⇒ Object



125
126
127
# File 'app/models/application_type.rb', line 125

def all(*arguments)
  find(:all, *arguments)
end

.custom(attrs = {}) ⇒ Object



163
164
165
166
167
# File 'app/models/application_type.rb', line 163

def custom(attrs={})
  attrs = {} if attrs.nil? || attrs.is_a?(String)
  attrs[:scalable] = true unless attrs.has_key?(:scalable)
  new(:id => 'custom', :display_name => 'From Scratch').assign_attributes(attrs)
end

.find(*arguments) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/application_type.rb', line 137

def find(*arguments)
  option = arguments.shift
  case option
  when String
    find_single(option, *arguments)
  when :all
    find_every(*arguments)
  when Symbol
    find_every(*arguments).select { |t| t.tags.include? option }
  else
    raise "Unsupported scope"
  end
end

.matching_cartridges(cartridge_specs) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/application_type.rb', line 151

def matching_cartridges(cartridge_specs)
  valid, invalid = {}, []
  Array(cartridge_specs).uniq.each do |c|
    if (matches = CartridgeType.cached.matches(c)).present?
      valid[c] = matches
    else
      invalid << c
    end
  end
  [valid, invalid]
end

.search(query, opts = {}) ⇒ Object



129
130
131
# File 'app/models/application_type.rb', line 129

def search(query, opts={})
  find(:all, {:search => query}.merge(opts))
end

.tagged(tag, opts = {}) ⇒ Object



133
134
135
# File 'app/models/application_type.rb', line 133

def tagged(tag, opts={})
  find(:all, {:tag => tag}.merge(opts))
end

.user_tags(tags) ⇒ Object



8
9
10
# File 'app/models/application_type.rb', line 8

def self.user_tags(tags)
  tags - PROTECTED_TAGS
end

Instance Method Details

#<=>(other) ⇒ Object



74
75
76
77
78
79
80
81
# File 'app/models/application_type.rb', line 74

def <=>(other)
  return 0 if id == other.id
  c = source_priority - other.source_priority
  return c unless c == 0
  c = priority - other.priority
  return c unless c == 0
  display_name <=> other.display_name
end

#>>(app) ⇒ Object



105
106
107
108
109
110
# File 'app/models/application_type.rb', line 105

def >>(app)
  app.cartridges = cartridges if cartridges.present?
  app.initial_git_url = initial_git_url if initial_git_url
  app.initial_git_branch = initial_git_branch if initial_git_branch
  app
end

#assign_attributes(values, options = {}) ⇒ Object

Default mass assignment support



115
116
117
118
119
120
# File 'app/models/application_type.rb', line 115

def assign_attributes(values, options = {})
  sanitize_for_mass_assignment(values, options[:as]).each do |k, v|
    send("#{k}=", v)
  end
  self
end

#cartridge?Boolean

Returns:

  • (Boolean)


98
# File 'app/models/application_type.rb', line 98

def cartridge?; source == :cartridge; end

#cartridge_specsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/application_type.rb', line 56

def cartridge_specs
  begin
    s = (@cartridges_spec || cartridges || [])
    if s.is_a? Array
      s
    else
      s = s.strip
      if s[0] == '['
        ActiveSupport::JSON.decode(s).map{ |s| s.is_a?(Hash) ? s['name'] : s }
      else
        s.split(',').map(&:strip)
      end
    end
  rescue
    raise ApplicationType::CartridgeSpecInvalid, $!, $@
  end
end

#matching_cartridgesObject



101
102
103
# File 'app/models/application_type.rb', line 101

def matching_cartridges
  self.class.matching_cartridges(cartridge_specs)
end

#new?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/application_type.rb', line 44

def new?
  tags.include?(:new)
end

#persisted?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/models/application_type.rb', line 40

def persisted?
  @persisted
end

#quickstart?Boolean

Returns:

  • (Boolean)


99
# File 'app/models/application_type.rb', line 99

def quickstart?; source == :quickstart; end

#source_priorityObject



91
92
93
94
95
96
# File 'app/models/application_type.rb', line 91

def source_priority
  case source
  when :cartridge; -2
  else; 0
  end
end

#to_paramsObject



48
49
50
# File 'app/models/application_type.rb', line 48

def to_params
  persisted? ? {:id => id} : [:cartridges, :initial_git_url, :initial_git_branch].inject({}){ |h, s| h[s] = send(s); h }
end