Module: EditModeRails::ActionViewExtensions::EditModeHelper

Defined in:
lib/editmode-rails/action_view_extensions/editmode_helper.rb

Instance Method Summary collapse

Instance Method Details

#api_root_urlObject



12
13
14
# File 'lib/editmode-rails/action_view_extensions/editmode_helper.rb', line 12

def api_root_url
  ENV["EDITMODE_OVERRIDE_API_URL"] || "https://www.editmode.app/api"
end

#api_versionObject



8
9
10
# File 'lib/editmode-rails/action_view_extensions/editmode_helper.rb', line 8

def api_version
  "v1"
end

#bit(label, identifier, options = {}) ⇒ Object



116
117
118
# File 'lib/editmode-rails/action_view_extensions/editmode_helper.rb', line 116

def bit(label,identifier,options={})
  chunk_display(label,identifier,options)
end

#chunk(label, identifier, options = {}) ⇒ Object



120
121
122
# File 'lib/editmode-rails/action_view_extensions/editmode_helper.rb', line 120

def chunk(label,identifier,options={})
  chunk_display(label,identifier,options)
end

#chunk_collection(collection_identifier, has_tags = []) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/editmode-rails/action_view_extensions/editmode_helper.rb', line 20

def chunk_collection(collection_identifier,has_tags=[])
  begin 
    url = "#{versioned_api_url}/chunks?collection_identifier=#{collection_identifier}"
    response = HTTParty.get(url)
    chunks = response["chunks"]
    return chunks
  rescue => error
    puts error 
    []
  end
end

#chunk_display(label, identifier, options = {}, custom_field_info = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/editmode-rails/action_view_extensions/editmode_helper.rb', line 46

def chunk_display(label,identifier,options={},custom_field_info={})
  
  begin 

    if custom_field_info.present?
      chunk_content = custom_field_info["value"]
    else

      cache_identifier = "chunk_#{identifier}"
      url = "#{versioned_api_url}/bits/#{identifier}"

      if !Rails.cache.exist?(cache_identifier)
        response = HTTParty.get(url)
      end
        
      chunk_content = Rails.cache.fetch(cache_identifier) do  
        response['content']
      end

      chunk_type = Rails.cache.fetch("#{cache_identifier}_type") do  
        response['chunk_type']
      end

    end

    if chunk_type == "image"
      display_type = "image"
    else 
      display_type = options[:display_type] || "span"
    end
    css_class = options[:css_class]
    content_type = "plain"

    # Simple check to see if returned chunk contains html. Regex will need to be improved
    if /<[a-z][\s\S]*>/i.match(chunk_content)
      content_type = "rich"
      chunk_content = sanitize chunk_content.html_safe
    elsif chunk_content.include? "\n"
      content_type = "rich"
      renderer = Redcarpet::Render::HTML.new(no_links: true, hard_wrap: true)
      markdown = Redcarpet::Markdown.new(renderer, extensions = {})
      chunk_content = markdown.render(chunk_content).html_safe
    end

    additional_data_properties = custom_field_info.present? ? { :custom_field_identifier => custom_field_info["custom_field_identifier"] } : {}

    case display_type
    when "span"
      if content_type == "rich"
        (:span, :class => css_class, :data => {:chunk => identifier, :chunk_editable => false}.merge(additional_data_properties) ) do
          chunk_content
        end
      else
        (:span, :class => css_class, :data => {:chunk => identifier, :chunk_editable => true}.merge(additional_data_properties)) do
          chunk_content
        end
      end
    when "raw"
      chunk_content
    when "image"
      (:span, :data => {:chunk => identifier, :chunk_editable => false}.merge(additional_data_properties)) do 
        image_tag(chunk_content, :class => css_class) 
      end
    end
 rescue => error
  puts error
 end

end

#chunk_property(chunk_info, custom_field_identifier = nil, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/editmode-rails/action_view_extensions/editmode_helper.rb', line 32

def chunk_property(chunk_info,custom_field_identifier=nil,options={})

  chunk_identifier = chunk_info["identifier"]

  if custom_field_identifier
    custom_field_info = chunk_info["custom_fields"].select{|custom_field| custom_field["custom_field_identifier"] == custom_field_identifier }[0]
    if custom_field_info.present? 
      chunk_display("",chunk_identifier,options,custom_field_info)
    end
  else 
    chunk_display("",chunk_identifier,options)
  end
end

#raw_chunk(label, identifier, options = {}) ⇒ Object



124
125
126
# File 'lib/editmode-rails/action_view_extensions/editmode_helper.rb', line 124

def raw_chunk(label,identifier,options={})
  chunk_display(label,identifier,options.merge(:display_type => "raw"))
end

#versioned_api_urlObject



16
17
18
# File 'lib/editmode-rails/action_view_extensions/editmode_helper.rb', line 16

def versioned_api_url
  "#{api_root_url}/#{api_version}"
end