Class: Hatio::Generators::ApiUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/hatio/util/api_util.rb

Class Method Summary collapse

Class Method Details

.generate_index_json_builder(singular_name, all_attrs) ⇒ Object

index.jbuilder 에서 사용할 json builder



70
71
72
73
# File 'lib/generators/hatio/util/api_util.rb', line 70

def self.generate_index_json_builder(singular_name, all_attrs)
  all_col_str = all_attrs.map { |col| ":#{col.name}" }.join(",")
  "json.(#{singular_name}, #{all_col_str})\n"
end

.generate_index_template(singular_name, columns) ⇒ Object

index template 생성



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
# File 'lib/generators/hatio/util/api_util.rb', line 7

def self.generate_index_template(singular_name, columns)
  output = "json.items do |json|\n"
  output << "\tjson.array!(@collection) do |#{singular_name}|\n"
  output << generate_index_json_builder(singular_name, columns)
  ref_columns = columns.select { |column| column.name != 'domain_id' && column.ref_type == 'Entity' && column.ref_name }
    
  ref_columns.each do |ref_column|
    next if (!ref_column.list_rank || ref_column.list_rank <= 0)
    ref_singular_name = ref_column.ref_name.underscore

    if(ref_column.name == 'creator_id')        
      output << "\n\t\tjson.creator do"
      output << "\n\t\t\tjson.id #{singular_name}.creator_id"
      output << "\n\t\t\tjson.name #{singular_name}.creator ? #{singular_name}.creator.name : ''"
      output << "\n\t\tend\n"
    elsif(ref_column.name == 'updater_id')
      output << "\n\t\tjson.updater do"
      output << "\n\t\t\tjson.id #{singular_name}.updater_id"
      output << "\n\t\t\tjson.name #{singular_name}.updater ? #{singular_name}.updater.name : ''"
      output << "\n\t\tend\n"
    elsif(ref_column.name.ends_with?('_id'))
      output << "\n\t\tjson.#{ref_singular_name} do"
      output << "\n\t\t\tjson.id #{singular_name}.#{ref_column.name}"
      output << "\n\t\t\tjson.name #{singular_name}.#{ref_singular_name} ? #{singular_name}.#{ref_singular_name}.name : ''"
      output << "\n\t\tend\n"
    end
  end
    
  output << "\tend\n"
  output << "end\n"
  output << "json.total @total_count\n"
  output << "json.success true\n"
  output
end

.generate_ref_entity_json_builder(singular_name, biz_attrs) ⇒ Object

show.jbuilder 에서 사용, entity reference type인 경우



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/generators/hatio/util/api_util.rb', line 78

def self.generate_ref_entity_json_builder(singular_name, biz_attrs)
  ref_columns, output = biz_attrs.select { |col| col.ref_type == 'Entity' && col.ref_name }, ""
  ref_columns.each do |ref_column|
    next if (!ref_column.list_rank || ref_column.list_rank <= 0)
    ref_singular_name = ref_column.ref_name.underscore
    ref_class = ref_column.ref_name.constantize
    output << "\njson.#{ref_singular_name} do"
    output << "\n\tjson.id @#{singular_name}.#{ref_column.name}"
    output << "\n\tjson.name @#{singular_name}.#{ref_singular_name} ? @#{singular_name}.#{ref_singular_name}.name : ''"
    if(ref_class.column_names.include?("description"))
      output << "\n\tjson.description @#{singular_name}.#{ref_singular_name} ? @#{singular_name}.#{ref_singular_name}.description : ''"
    end
    output << "\nend\n"
  end
  output
end

.generate_show_json_builder(singular_name, all_attrs) ⇒ Object

show.jbuilder 에서 사용할 json builder



53
54
55
56
# File 'lib/generators/hatio/util/api_util.rb', line 53

def self.generate_show_json_builder(singular_name, all_attrs)
  all_col_str = all_attrs.find_all { |col| (col.name != 'creator_id' && col.name != 'updater_id') }.map { |col| ":#{col.name}" }.join(",")
  "json.(@#{singular_name}, #{all_col_str})"
end

.generate_single_json_builder(singular_name, all_attrs) ⇒ Object

create, update.json.jbuilder 에서 사용할 json builder



45
46
47
48
# File 'lib/generators/hatio/util/api_util.rb', line 45

def self.generate_single_json_builder(singular_name, all_attrs)
  all_col_str = all_attrs.map { |col| ":#{col.name}" }.join(",")
  "json.(@#{singular_name}, #{all_col_str})"
end

.generate_userstamp_json_builder(singular_name, all_attrs) ⇒ Object

show.jbuilder 에서 사용할 userstamp용 json builder



61
62
63
64
65
# File 'lib/generators/hatio/util/api_util.rb', line 61

def self.generate_userstamp_json_builder(singular_name, all_attrs)
  output = "json.creator @#{singular_name}.creator, :id, :name if @#{singular_name}.creator\n"
  output << "json.updater @#{singular_name}.updater, :id, :name if @#{singular_name}.updater"
  output
end