Module: LatoBlog::Interface::Fields

Included in:
LatoBlog::Interface
Defined in:
lib/lato_blog/interfaces/fields.rb

Instance Method Summary collapse

Instance Method Details

#blog__create_db_post_field(post, key, content, post_field_parent = nil) ⇒ Object

This function creates a new db post field from a specific content.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lato_blog/interfaces/fields.rb', line 61

def blog__create_db_post_field(post, key, content, post_field_parent = nil)
  # create post field on database
  db_post_field = LatoBlog::PostField.new(
    key: key,
    typology: content[:type],
    lato_blog_post_id: post.id,
    lato_blog_post_field_id: post_field_parent ? post_field_parent.id : nil
  )
  throw "Impossible to create post field #{key}" unless db_post_field.save
  # update post field with correct content
  blog__update_db_post_field(db_post_field, content, post_field_parent)
end

#blog__sync_config_post_field(post, key, content) ⇒ Object

This function syncronizes a single post field of a specific post with database.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lato_blog/interfaces/fields.rb', line 25

def blog__sync_config_post_field(post, key, content)
  db_post_field = LatoBlog::PostField.find_by(
    key: key,
    lato_blog_post_id: post.id,
    lato_blog_post_field_id: nil
  )
  # check if post field can be created for the post
  if content[:categories] && !content[:categories].empty?
    db_categories = LatoBlog::Category.where(meta_permalink: content[:categories])
    return if (post.categories.pluck(:id) & db_categories.pluck(:id)).empty?
  end
  # run correct action for field
  if db_post_field
    blog__update_db_post_field(db_post_field, content)
  else
    blog__create_db_post_field(post, key, content)
  end
end

#blog__sync_config_post_fields_with_db_post_fieldsObject

This function syncronizes the config post fields with the post fields on database.



7
8
9
10
11
# File 'lib/lato_blog/interfaces/fields.rb', line 7

def blog__sync_config_post_fields_with_db_post_fields
  posts = LatoBlog::Post.all
  # create / update fields on database
  posts.map { |p| blog__sync_config_post_fields_with_db_post_fields_for_post(p) }
end

#blog__sync_config_post_fields_with_db_post_fields_for_post(post) ⇒ Object

This function syncronizes the config post fields with the post fields on database for a single post object.



15
16
17
18
19
20
21
22
# File 'lib/lato_blog/interfaces/fields.rb', line 15

def blog__sync_config_post_fields_with_db_post_fields_for_post(post)
  # save or update post fields from config
  post_fields = CONFIGS[:lato_blog][:post_fields]
  post_fields.map { |key, content| blog__sync_config_post_field(post, key, content) }
  # remove old post fields
  db_post_fields = post.post_fields.visibles.roots
  db_post_fields.map { |dbpf| blog__sync_db_post_field(post, dbpf) }
end

#blog__sync_db_post_field(post, db_post_field) ⇒ Object

This function syncronizes a single post field of a specific post with config file.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lato_blog/interfaces/fields.rb', line 45

def blog__sync_db_post_field(post, db_post_field)
  post_fields = CONFIGS[:lato_blog][:post_fields]
  # search db post field on config file
  content = post_fields[db_post_field.key]
  db_post_field.update(meta_visible: false) && return unless content
  # check category of post field is accepted
  if content[:categories] && !content[:categories].empty?
    db_categories = LatoBlog::Category.where(meta_permalink: content[:categories])
    db_post_field.update(meta_visible: false) && return if (post.categories.pluck(:id) & db_categories.pluck(:id)).empty?
  end
end

#blog__update_db_post_field(db_post_field, content, post_field_parent = nil) ⇒ Object

This function update an existing post field on database with new content.



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
# File 'lib/lato_blog/interfaces/fields.rb', line 75

def blog__update_db_post_field(db_post_field, content, post_field_parent = nil)
  # run minimum updates
  db_post_field.update(
    position: content[:position],
    meta_visible: true
  )
  # run custom update for type
  case db_post_field.typology
  when 'text'
    update_db_post_field_text(db_post_field, content, post_field_parent)
  when 'datetime'
    update_db_post_field_datetime(db_post_field, content, post_field_parent)
  when 'editor'
    update_db_post_field_editor(db_post_field, content, post_field_parent)
  when 'geolocalization'
    update_db_post_field_geolocalization(db_post_field, content, post_field_parent)
  when 'image'
    update_db_post_field_image(db_post_field, content, post_field_parent)
  when 'gallery'
    update_db_post_field_gallery(db_post_field, content, post_field_parent)
  when 'youtube'
    update_db_post_field_youtube(db_post_field, content, post_field_parent)
  when 'composed'
    update_db_post_field_composed(db_post_field, content, post_field_parent)
  when 'relay'
    update_db_post_field_relay(db_post_field, content, post_field_parent)
  end
end