Module: Roda::RodaPlugins::Assets::InstanceMethods

Defined in:
lib/roda/plugins/assets.rb

Instance Method Summary collapse

Instance Method Details

#assets(folder, options = {}) ⇒ Object

This will ouput the files with the appropriate tags



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/roda/plugins/assets.rb', line 120

def assets folder, options = {}
  attrs   = options.map{|k,v| "#{k}=\"#{v}\""}
  tags    = []
  folder  = [folder] unless folder.is_a? Array
  type    = folder.first
  attr    = type.to_s == 'js' ? 'src' : 'href'
  path    = "#{assets_opts[:route]}/#{assets_opts[:"#{type}_folder"]}"
  files   = folder.length == 1 \
          ? assets_opts[:"#{folder[0]}"] \
          : assets_opts[:"#{folder[0]}"][:"#{folder[1]}"]

  # Create a tag for each individual file
  if assets_opts[:compiled] || assets_opts[:concat]
    name = assets_opts[:compiled] ? assets_opts[:compiled_name] : assets_opts[:concat_name]
    name = "#{name}/#{folder.join('-')}"
    # Generate unique url so middleware knows to check for # compile/concat
    attrs.unshift("#{attr}=\"/#{path}/#{name}/#{assets_unique_id(type)}.#{type}\"")
    # Return tag string
    send("#{type}_assets_tag", attrs.join(' '))
  else
    files.each do |file|
      # This allows you to do things like:
      # assets_opts[:css] = ['app', './bower/jquery/jquery-min.js']
      file.gsub!(/\./, '$2E')
      # Add tags to the tags array
      tags << send(
        "#{type}_assets_tag",
        attrs.dup.unshift( "#{attr}=\"/#{path}/#{file}.#{type}\"").join(' ')
      )
    end
    # Return tags as string
    tags.join "\n"
  end
end

#assets_optsObject

Shortcut for class opts



221
222
223
# File 'lib/roda/plugins/assets.rb', line 221

def assets_opts
  self.class.assets_opts
end

#assets_unique_id(*args) ⇒ Object

Shortcut for class assets unique id



226
227
228
# File 'lib/roda/plugins/assets.rb', line 226

def assets_unique_id(*args)
  self.class.assets_unique_id(*args)
end

#read_asset_file(file, type) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/roda/plugins/assets.rb', line 184

def read_asset_file(file, type)
  folder = assets_opts[:"#{type}_folder"]

  # If there is no file it must be a remote file request.
  # Lets set the file to the url
  if file == ''
    route = assets_opts[:route]
    file  = env['SCRIPT_NAME'].gsub(/^\/#{route}\/#{folder}\//, '')
  end

  # If it's not a url or parent direct append the full path
  if !file[/^\.\//] && !file[/^http/]
    file = assets_opts[:path] + '/' + folder + "/#{file}"
  end

  # set the current engine
  engine = assets_opts[:"#{type}_engine"]

  if File.exists? "#{file}.#{engine}"
    # render via tilt
    render path: "#{file}.#{engine}"
  elsif File.exists? "#{file}.#{type}"
    # read file directly
    File.read "#{file}.#{type}"
  elsif file[/^http/]
    # grab remote file content
    Net::HTTP.get(URI.parse(file))
  elsif File.exists?(file) && !file[/\.#{type}$/]
    # Render via tilt if the type isn't css or js
    render path: file
  else
    # if it is css/js read the file directly
    File.read file
  end
end

#render_asset(file, type) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/roda/plugins/assets.rb', line 155

def render_asset(file, type)
  # convert back url safe to period
  file.gsub!(/(\$2E|%242E)/, '.')

  if !assets_opts[:compiled] && !assets_opts[:concat]
    read_asset_file file, type
  elsif assets_opts[:compiled]
    folder = file.split('/')[1].split('-', 2)
    path   = assets_opts[:compiled_path] \
           + "/#{assets_opts[:"#{type}_folder"]}/" \
           + assets_opts[:compiled_name] \
           + (folder.length > 1 ? ".#{folder[1]}" : '') \
           + ".#{type}"

    File.read path
  elsif assets_opts[:concat]
    # "concat.roda.assets/css/123"
    content   = ''
    folder = file.split('/')[1].split('-', 2)
    files  = folder.length == 1 \
            ? assets_opts[:"#{folder[0]}"] \
            : assets_opts[:"#{folder[0]}"][:"#{folder[1]}"]

    files.each { |f| content += read_asset_file f, type }

    content
  end
end