Module: Neofiles::NeofilesHelper

Defined in:
app/helpers/neofiles/neofiles_helper.rb

Instance Method Summary collapse

Instance Method Details

#neofiles_cdn_prefix(*args) ⇒ Object

Returns CDN (Content Delivery Network) prefix - mainly a domain - if available.

Array of CDNs is set via Rails.application.config.neofiles.cdns. If many exist, we choose one by taking remainder of dividing unix epoch creation time of the object, for which prefix is requested, by number of CDNs.

If no CDN available, will take current domain via Rails helper root_url.

First argument is considered Neofiles::File, ID or Hash. Other arguments are ignored.

Returned prefix is of form ‘doma.in/url/prefix’.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/helpers/neofiles/neofiles_helper.rb', line 112

def neofiles_cdn_prefix(*args)
  cdns = Rails.application.config.neofiles.cdns || []
  cdns << root_url unless cdns.any?

  if cdns.count > 1
    some_file = args.first
    if some_file.is_a? Neofiles::File
      gen_time = some_file.id.generation_time.sec
    elsif some_file.is_a?  Hash
      tmp = some_file[:id] || some_file['id'] || some_file[:_id] || some_file['_id'] || ""
      gen_time = BSON::ObjectId.legal?(tmp) ? BSON::ObjectId.from_string(tmp).generation_time.sec : Time.now.strftime('%U').to_i
    elsif some_file.is_a? String
      gen_time = BSON::ObjectId.legal?(some_file) ? BSON::ObjectId.from_string(some_file).generation_time.sec : Time.now.strftime('%U').to_i
    else
      gen_time = Time.now.strftime('%U').to_i
    end

    cdn = cdns[gen_time % cdns.count]
  else
    cdn = cdns.first
  end

  cdn.sub! /\/\z/, ''
  cdn = 'http://' + cdn unless cdn =~ /\Ahttp[s]?:\/\//
  cdn
end

#neofiles_file_url(*args) ⇒ Object

Override file URL generation to include CDN prefix.



140
141
142
# File 'app/helpers/neofiles/neofiles_helper.rb', line 140

def neofiles_file_url(*args)
  neofiles_cdn_prefix(*args) + neofiles_file_path(*args)
end

#neofiles_image_url(*args) ⇒ Object

Override image URL generation to include CDN prefix.



145
146
147
# File 'app/helpers/neofiles/neofiles_helper.rb', line 145

def neofiles_image_url(*args)
  neofiles_cdn_prefix(*args) + neofiles_image_path(*args)
end

Same as neofiles_img_tag but returned IMG is wrapped into A tag (HTML link) pointing to the file original.

link_attrs - HTML attrs for A
img_attrs  - HTML attrs for IMG

Other params are equivalent to neofiles_img_tag.



35
36
37
38
# File 'app/helpers/neofiles/neofiles_helper.rb', line 35

def neofiles_img_link(image_file, width = nil, height = nil, resize_options = {}, link_attrs = {}, img_attrs = {})
  link_attrs[:href] = neofiles_image_url image_file unless link_attrs[:href]
  neofiles_link(image_file, neofiles_img_tag(image_file, width, height, resize_options, img_attrs), link_attrs)
end

#neofiles_img_tag(image_file, width = nil, height = nil, resize_options = {}, html_attrs = {}) ⇒ Object

Returns HTML IMG tag.

image_file      - ID, Neofiles::Image of Hash
width, height   - if both are passed, image will be no more that that size
resize_options  - crop: '1'/'0' (change or preserve aspect ration, @see Neofiles::ImagesController#show)
html_attrs      - hash of additional HTML attrs like ALT, TITLE etc.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/neofiles/neofiles_helper.rb', line 10

def neofiles_img_tag(image_file, width = nil, height = nil, resize_options = {}, html_attrs = {})

  resize_options ||= {} # в gem_neo_adv мы передаем nil

  unless image_file.blank?
    resize_options  = resize_options.merge(format: [width.to_i, height.to_i].join('x')) if width.to_i > 0 && height.to_i > 0
    size_attrs      = resize_options.key?(:size_attrs) ? resize_options[:size_attrs] : true
    image_or_id     = image_file.is_a?(Hash) ? image_file[:id] : image_file

    html_attrs.try :symbolize_keys!
    html_attrs[:src] = neofiles_image_url image_or_id, resize_options

    html_attrs[:width], html_attrs[:height] = dimensions_after_resize(image_file, width.to_i, height.to_i, resize_options) if size_attrs
  end

  tag :img, html_attrs
end

Returns HTML A tag with link to the passed file.

file        - ID or Neofiles::File
tag_content - what the link content will be (default: file description of filename)
html_attrs  - additional HTML attrs like TITLE, TARGET etc.


46
47
48
49
# File 'app/helpers/neofiles/neofiles_helper.rb', line 46

def neofiles_link(file, tag_content = nil, html_attrs = {})
  html_attrs[:href] = neofiles_file_url file unless html_attrs[:href]
  (:a, tag_content.presence || file.description.presence || file.filename, html_attrs)
end

#swf_embed(id, url, width, height, bgcolor, click_tag, alt = '') ⇒ Object

Returns HTML OBJECT tag to embed SWF (Flash) files.

For fully crossbrowser experience include swfobject.js on page, where SWF embedding is performed.

id        - DOM ID of the object
url       - path/url to SWF file
width     - resulting object's area width
height    - resulting object's area height
bgcolor   - if passed, the object's area will be colored in this CSS color
click_tag - clickTAG is a common name for Flash variable used to tell a movie clip to redirect viewer to a certain
            URL after clicking (used in banners)
alt       - alternative HTML, in case if Flash is not available


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/helpers/neofiles/neofiles_helper.rb', line 64

def swf_embed(id, url, width, height, bgcolor, click_tag, alt = '')
  url = h(url)
  click_tag = h(click_tag)

  result = <<HTML
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="#{width}" height="#{height}" id="#{id}">
      <param name="movie" value="#{url}" />
      <param name="bgcolor" value="#{bgcolor}" />
      <param name="wmode" value="opaque" />
      <param name="allowfullscreen" value="false" />
      <param name="allowscriptaccess" value="never" />
      <param name="quality" value="autohigh" />
      <param name="flashvars" value="clickTAG=#{click_tag}" />

      <!--[if !IE]>-->
        <object type="application/x-shockwave-flash" data="#{url}" width="#{width}" height="#{height}">
          <param name="bgcolor" value="#{bgcolor}" />
          <param name="wmode" value="opaque" />
          <param name="allowfullscreen" value="false" />
          <param name="allowscriptaccess" value="never" />
          <param name="quality" value="autohigh" />
          <param name="flashvars" value="clickTAG=#{click_tag}" />
      <!--<![endif]-->

      #{alt}

      <!--[if !IE]>-->
        </object>
      <!--<![endif]-->
    </object>
    <script type="text/javascript">
      try { swfobject.registerObject("#{id}", "9.0.0"); } catch(e) {}
  </script>
HTML
  result.html_safe
end