Module: Hancock::Admin

Defined in:
lib/hancock_cms.rb,
lib/hancock/admin.rb,
lib/hancock/admin/embedded_element.rb

Defined Under Namespace

Modules: EmbeddedElement

Class Method Summary collapse

Class Method Details

.categories_block(is_active = false, options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hancock/admin.rb', line 93

def self.categories_block(is_active = false, options = {})
  if is_active.is_a?(Hash)
    is_active, options = (is_active[:active] || false), is_active
  end
  fields = (options[:fields] ||= {})
  field_names = [:main_category, :categories]
  field_showings = get_field_showings(fields, field_names)

  Proc.new {
    active is_active
    label options[:label] || I18n.t('hancock.categories')

    field :main_category do
      inline_add false
      inline_edit false
    end if field_showings[:main_category]
    field :categories, :hancock_multiselect if field_showings[:categories]

    Hancock::RailsAdminGroupPatch::hancock_cms_group(self, options[:fields] || {})

    if block_given?
      yield self
    end
  }
end

.content_block(is_active = false, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hancock/admin.rb', line 71

def self.content_block(is_active = false, options = {})
  if is_active.is_a?(Hash)
    is_active, options = (is_active[:active] || false), is_active
  end
  fields = (options[:fields] ||= {})
  field_names = [:excerpt, :content]
  field_showings = get_field_showings(fields, field_names)

  Proc.new {
    active is_active
    label options[:label] || I18n.t('hancock.content')
    field :excerpt, :hancock_html  if field_showings[:excerpt]
    field :content, :hancock_html  if field_showings[:content]

    Hancock::RailsAdminGroupPatch::hancock_cms_group(self, options[:fields] || {})

    if block_given?
      yield self
    end
  }
end

.get_field_showings(fields, field_names) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/hancock/admin.rb', line 175

def self.get_field_showings(fields, field_names)
  field_showings = field_names.map { |f| {f => true } }.inject(&:merge) || {}
  if fields.is_a?(Hash)
    field_names.each do |f|
      field_showings[f] = (fields[f] != false)
    end
  elsif fields.is_a?(Array)
    _fields = fields.map { |f| f[:fields] }.inject(&:merge) || {}
    field_names.each do |f|
      field_showings[f] = (_fields[f] != false)
    end
  end
  field_showings
end

.insertions_block(is_active = false, options = {}) ⇒ Object



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
# File 'lib/hancock/admin.rb', line 120

def self.insertions_block(is_active = false, options = {})
  if is_active.is_a?(Hash)
    is_active, options = (is_active[:active] || false), is_active
  end
  fields = (options[:fields] ||= {})
  field_names = [:possible_insertions]
  field_showings = get_field_showings(fields, field_names)

  Proc.new {
    active is_active
    label options[:label] || I18n.t('hancock.insertions')
    field :possible_insertions do
      read_only true
      pretty_value do
        ("<dl class='possible_insertions_list'>" + bindings[:object].possible_insertions.map do |_ins|
          "<dt>#{_ins}</dt><dd>#{bindings[:object].send(_ins)}</dd>"
        end.join + "</dl>").html_safe
      end
    end if field_showings[:possible_insertions]

    Hancock::RailsAdminGroupPatch::hancock_cms_group(self, options[:fields] || {})

    if block_given?
      yield self
    end
  }
end

.map_config(is_active = false, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hancock/admin.rb', line 3

def self.map_config(is_active = false, options = {})
  if is_active.is_a?(Hash)
    is_active, options = (is_active[:active] || false), is_active
  end
  fields = (options[:fields] ||= {})
  field_names = [:address, :map_address, :map_hint, :coordinates, :lat, :lon]
  field_showings = get_field_showings(fields, field_names)

  Proc.new {
    active is_active
    label options[:label] || I18n.t('hancock.map')

    field :address, :string do
      searchable true
    end if field_showings[:address]

    field :map_address, :string do
      searchable true
    end if field_showings[:map_address]

    field :map_hint, :string do
      searchable true
    end if field_showings[:map_hint]

    field :coordinates, :string do
      searchable true
      read_only true
      formatted_value{ bindings[:object].coordinates.to_json }
    end if field_showings[:coordinates]

    field :lat do
      searchable true
    end if field_showings[:lat]

    field :lon do
      searchable true
    end if field_showings[:lon]

    Hancock::RailsAdminGroupPatch::hancock_cms_group(self, options[:fields])

    if block_given?
      yield self
    end
  }
end

.url_block(is_active = false, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hancock/admin.rb', line 49

def self.url_block(is_active = false, options = {})
  if is_active.is_a?(Hash)
    is_active, options = (is_active[:active] || false), is_active
  end
  fields = (options[:fields] ||= {})
  field_names = [:slugs, :text_slug]
  field_showings = get_field_showings(fields, field_names)

  Proc.new {
    active is_active
    label options[:label] || I18n.t('hancock.url')
    field :slugs, :hancock_slugs  if field_showings[:slugs]
    field :text_slug              if field_showings[:text_slug]

    Hancock::RailsAdminGroupPatch::hancock_cms_group(self, options[:fields])

    if block_given?
      yield self
    end
  }
end

.user_defined_field_block(field_name, is_active = false, options = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/hancock/admin.rb', line 148

def self.user_defined_field_block(field_name, is_active = false, options = {})
  if field_name.is_a?(Hash)
    field_name, is_active, options = (field_name[:field_name] || field_name[:field]), (field_name[:active] || false), field_name
  elsif is_active.is_a?(Hash)
    is_active, options = (is_active[:active] || false), is_active
  end
  field_name = field_name.to_sym
  render_method = options[:render_method] || "render_#{field_name}"

  fields = (options[:fields] ||= {})
  field_names = [field_name, render_method]
  field_showings = get_field_showings(fields, field_names)

  Proc.new {
    active is_active
    label options[:label]# || I18n.t('hancock.user_defined_field')
    field @abstract_model.model.user_defined_fields[field_name][:render_method], :toggle if field_showings[render_method]
    field field_name, :ck_editor if field_showings[field_name]

    Hancock::RailsAdminGroupPatch::hancock_cms_group(self, options[:fields] || {})

    if block_given?
      yield self
    end
  }
end